OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 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 #ifndef THIRD_PARTY_ZLIB_GOOGLE_ZIP_WRITER_H_ |
| 5 #define THIRD_PARTY_ZLIB_GOOGLE_ZIP_WRITER_H_ |
| 6 |
| 7 #include <map> |
| 8 |
| 9 #include "base/strings/string_piece.h" |
| 10 |
| 11 namespace base { |
| 12 class FilePath; |
| 13 class RefCountedMemory; |
| 14 } |
| 15 |
| 16 namespace zip { |
| 17 |
| 18 // This class is used for creating or modifying zip files. |
| 19 // |
| 20 // ZipWriter writer; |
| 21 // writer.AddFile(<path>,<contents>); |
| 22 // writer.DeleteFile(<path>); |
| 23 // writer.AddFolder(<path>); |
| 24 // writer.DeleteFolder(<path>); |
| 25 // writer.Commit(<zip path>,Append/OverWrite); |
| 26 // |
| 27 // All modifications are batched and executed in one shot during the call to |
| 28 // Commit. Commit() is the only method that does file IO. The target zip file |
| 29 // to modify will be rewritten from scratch, if contents need to be modified |
| 30 // or removed. |
| 31 // |
| 32 // Many calls to Add{File,Folder}/Delete{File,Folder} with the same path will |
| 33 // override the previous ones, so only the last modification applies. |
| 34 // |
| 35 // IMPORTANT: ZipWriter does not copy nor take ownership of any of the passed |
| 36 // file contents. It's the responsibility of the caller to ensure any pointer |
| 37 // passed is valid while the ZipWriter object is kept alive, or until Clear() |
| 38 // is called. |
| 39 // |
| 40 class ZipWriter { |
| 41 public: |
| 42 enum CommitMode { |
| 43 // Overwrites any existing file |
| 44 OverWrite, |
| 45 // Appends to existing file |
| 46 Append |
| 47 }; |
| 48 |
| 49 typedef std::map<const base::FilePath, base::StringPiece> ZipContents; |
| 50 typedef ZipContents::const_iterator const_iterator; |
| 51 |
| 52 ZipWriter(); |
| 53 |
| 54 // Adds a new file to the archive, or replaces if it already exists. |
| 55 // |file_path| is the relative path used to name the file in the archive. |
| 56 // |contents| is the contents of the new file. |
| 57 void AddFile(const base::FilePath& file_path, |
| 58 const base::StringPiece& contents); |
| 59 void AddFile(const base::FilePath& file_path, |
| 60 const base::RefCountedMemory* contents); |
| 61 |
| 62 // Adds a new folder to the archive, or replaces if it already exists. |
| 63 // |folder_path| is the relative path used to name the folder in the archive. |
| 64 void AddFolder(const base::FilePath& folder_path); |
| 65 |
| 66 // Marks the file with the given |file_path| to be deleted from the archive. |
| 67 void DeleteFile(const base::FilePath& file_path); |
| 68 |
| 69 // Marks the file with the given |folder_path| to be deleted from the archive. |
| 70 void DeleteFolder(const base::FilePath& folder_path); |
| 71 |
| 72 // Drops any Adds or Deletes for the given |file_or_folder_path| so further |
| 73 // calls to Commit() will not modify that file or folder. |
| 74 void DropModifications(const base::FilePath& file_or_folder_path); |
| 75 |
| 76 // Commits all changes to |zip_file|. The file will be created from scratch |
| 77 // or appended regarding the value or |mode|. This function does file IO. |
| 78 bool Commit(const base::FilePath& zip_file, CommitMode mode) const; |
| 79 |
| 80 // Clears all pending changes, making this ZipWriter empty. Any pointer kept |
| 81 // internally passed to AddFile is dropped. Calling Commit() with an empty |
| 82 // ZipWriter has no effect. |
| 83 void Clear(); |
| 84 |
| 85 const_iterator begin() const { |
| 86 return contents_.begin(); |
| 87 } |
| 88 |
| 89 const_iterator end() const { |
| 90 return contents_.end(); |
| 91 } |
| 92 |
| 93 private: |
| 94 #if __cplusplus >= 201103L |
| 95 // Trick to prevent temporary strings being passed to AddFile, because |
| 96 // ZipWriter does not copy the data, so deleted data would be used later. |
| 97 // This just causes a compilation failure. |
| 98 template<int x = 0> |
| 99 void AddFile(const base::FilePath&, const std::string&&); |
| 100 #endif |
| 101 |
| 102 bool HasFile(const base::FilePath& file_path) const; |
| 103 bool HasFolder(const base::FilePath& folder_path) const; |
| 104 |
| 105 void CheckValidFilePath(const base::FilePath& folder_path) const; |
| 106 void CheckValidFolderPath(const base::FilePath& folder_path) const; |
| 107 |
| 108 // Set of files to add or delete. |
| 109 ZipContents contents_; |
| 110 |
| 111 DISALLOW_COPY_AND_ASSIGN(ZipWriter); |
| 112 }; |
| 113 |
| 114 } // namespace zip |
| 115 |
| 116 #endif // THIRD_PARTY_ZLIB_GOOGLE_ZIP_WRITER_H_ |
OLD | NEW |