OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "third_party/zlib/google/zip_writer.h" |
| 6 |
| 7 #include "base/files/file_path.h" |
| 8 #include "base/logging.h" |
| 9 #include "base/memory/ref_counted_memory.h" |
| 10 #include "third_party/zlib/google/zip.h" |
| 11 #include "third_party/zlib/google/zip_internal.h" |
| 12 |
| 13 namespace zip { |
| 14 |
| 15 ZipWriter::ZipWriter() {} |
| 16 |
| 17 void ZipWriter::AddFile(const base::FilePath& file_path, |
| 18 const base::StringPiece& contents) { |
| 19 DCHECK(contents.data()); |
| 20 CheckValidFilePath(file_path); |
| 21 contents_[file_path] = contents; |
| 22 } |
| 23 |
| 24 void ZipWriter::AddFile(const base::FilePath& file_path, |
| 25 const base::RefCountedMemory* contents) { |
| 26 DCHECK(contents); |
| 27 AddFile(file_path, |
| 28 base::StringPiece(contents->front_as<char>(), contents->size())); |
| 29 } |
| 30 |
| 31 void ZipWriter::AddFolder(const base::FilePath& folder_path) { |
| 32 CheckValidFolderPath(folder_path); |
| 33 contents_[folder_path.AsEndingWithSeparator()] = base::StringPiece("", 0); |
| 34 } |
| 35 |
| 36 void ZipWriter::DeleteFile(const base::FilePath& file_path) { |
| 37 CheckValidFilePath(file_path); |
| 38 contents_[file_path] = base::StringPiece(); |
| 39 } |
| 40 |
| 41 void ZipWriter::DeleteFolder(const base::FilePath& folder_path) { |
| 42 CheckValidFolderPath(folder_path); |
| 43 contents_[folder_path.AsEndingWithSeparator()] = base::StringPiece(); |
| 44 } |
| 45 |
| 46 void ZipWriter::DropModifications(const base::FilePath& file_or_folder_path) { |
| 47 ZipContents::iterator position = contents_.find(file_or_folder_path); |
| 48 if (position == contents_.end()) |
| 49 position = contents_.find(file_or_folder_path.EndsWithSeparator() ? |
| 50 file_or_folder_path.StripTrailingSeparators() : |
| 51 file_or_folder_path.AsEndingWithSeparator()); |
| 52 if (position != contents_.end()) |
| 53 contents_.erase(position); |
| 54 } |
| 55 |
| 56 bool ZipWriter::Commit(const base::FilePath& zip_file, CommitMode mode) const { |
| 57 return internal::ZipFromMemory(zip_file, contents_, mode == Append); |
| 58 } |
| 59 |
| 60 void ZipWriter::Clear() { |
| 61 contents_.clear(); |
| 62 } |
| 63 |
| 64 bool ZipWriter::HasFile(const base::FilePath& file_path) const { |
| 65 return contents_.find(file_path.StripTrailingSeparators()) != contents_.end(); |
| 66 } |
| 67 bool ZipWriter::HasFolder(const base::FilePath& folder_path) const { |
| 68 return contents_.find(folder_path.AsEndingWithSeparator()) != contents_.end(); |
| 69 } |
| 70 |
| 71 void ZipWriter::CheckValidFilePath(const base::FilePath& path) const { |
| 72 DCHECK(!path.empty() && !path.IsAbsolute() && !path.ReferencesParent()); |
| 73 DCHECK(!path.EndsWithSeparator()); |
| 74 DCHECK(!HasFolder(path)); |
| 75 } |
| 76 void ZipWriter::CheckValidFolderPath(const base::FilePath& path) const { |
| 77 DCHECK(!path.empty() && !path.IsAbsolute() && !path.ReferencesParent()); |
| 78 DCHECK(!HasFile(path)); |
| 79 } |
| 80 |
| 81 } // namespace zip |
OLD | NEW |