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_READER_H_ |
| 5 #define THIRD_PARTY_ZLIB_GOOGLE_ZIP_READER_H_ |
| 6 |
| 7 #include <stddef.h> |
| 8 #include <stdint.h> |
| 9 |
| 10 #include <memory> |
| 11 #include <string> |
| 12 |
| 13 #include "base/callback.h" |
| 14 #include "base/files/file.h" |
| 15 #include "base/files/file_path.h" |
| 16 #include "base/files/file_util.h" |
| 17 #include "base/macros.h" |
| 18 #include "base/memory/weak_ptr.h" |
| 19 #include "base/time/time.h" |
| 20 |
| 21 #if defined(USE_SYSTEM_MINIZIP) |
| 22 #include <minizip/unzip.h> |
| 23 #else |
| 24 #include "third_party/zlib/contrib/minizip/unzip.h" |
| 25 #endif |
| 26 |
| 27 namespace zip { |
| 28 |
| 29 // A delegate interface used to stream out an entry; see |
| 30 // ZipReader::ExtractCurrentEntry. |
| 31 class WriterDelegate { |
| 32 public: |
| 33 virtual ~WriterDelegate() {} |
| 34 |
| 35 // Invoked once before any data is streamed out to pave the way (e.g., to open |
| 36 // the output file). Return false on failure to cancel extraction. |
| 37 virtual bool PrepareOutput() = 0; |
| 38 |
| 39 // Invoked to write the next chunk of data. Return false on failure to cancel |
| 40 // extraction. |
| 41 virtual bool WriteBytes(const char* data, int num_bytes) = 0; |
| 42 }; |
| 43 |
| 44 // This class is used for reading zip files. A typical use case of this |
| 45 // class is to scan entries in a zip file and extract them. The code will |
| 46 // look like: |
| 47 // |
| 48 // ZipReader reader; |
| 49 // reader.Open(zip_file_path); |
| 50 // while (reader.HasMore()) { |
| 51 // reader.OpenCurrentEntryInZip(); |
| 52 // reader.ExtractCurrentEntryToDirectory(output_directory_path); |
| 53 // reader.AdvanceToNextEntry(); |
| 54 // } |
| 55 // |
| 56 // For simplicity, error checking is omitted in the example code above. The |
| 57 // production code should check return values from all of these functions. |
| 58 // |
| 59 // This calls can also be used for random access of contents in a zip file |
| 60 // using LocateAndOpenEntry(). |
| 61 // |
| 62 class ZipReader { |
| 63 public: |
| 64 // A callback that is called when the operation is successful. |
| 65 typedef base::Closure SuccessCallback; |
| 66 // A callback that is called when the operation fails. |
| 67 typedef base::Closure FailureCallback; |
| 68 // A callback that is called periodically during the operation with the number |
| 69 // of bytes that have been processed so far. |
| 70 typedef base::Callback<void(int64_t)> ProgressCallback; |
| 71 |
| 72 // This class represents information of an entry (file or directory) in |
| 73 // a zip file. |
| 74 class EntryInfo { |
| 75 public: |
| 76 EntryInfo(const std::string& filename_in_zip, |
| 77 const unz_file_info& raw_file_info); |
| 78 |
| 79 // Returns the file path. The path is usually relative like |
| 80 // "foo/bar.txt", but if it's absolute, is_unsafe() returns true. |
| 81 const base::FilePath& file_path() const { return file_path_; } |
| 82 |
| 83 // Returns the size of the original file (i.e. after uncompressed). |
| 84 // Returns 0 if the entry is a directory. |
| 85 // Note: this value should not be trusted, because it is stored as metadata |
| 86 // in the zip archive and can be different from the real uncompressed size. |
| 87 int64_t original_size() const { return original_size_; } |
| 88 |
| 89 // Returns the last modified time. If the time stored in the zip file was |
| 90 // not valid, the unix epoch will be returned. |
| 91 // |
| 92 // The time stored in the zip archive uses the MS-DOS date and time format. |
| 93 // http://msdn.microsoft.com/en-us/library/ms724247(v=vs.85).aspx |
| 94 // As such the following limitations apply: |
| 95 // * only years from 1980 to 2107 can be represented. |
| 96 // * the time stamp has a 2 second resolution. |
| 97 // * there's no timezone information, so the time is interpreted as local. |
| 98 base::Time last_modified() const { return last_modified_; } |
| 99 |
| 100 // Returns true if the entry is a directory. |
| 101 bool is_directory() const { return is_directory_; } |
| 102 |
| 103 // Returns true if the entry is unsafe, like having ".." or invalid |
| 104 // UTF-8 characters in its file name, or the file path is absolute. |
| 105 bool is_unsafe() const { return is_unsafe_; } |
| 106 |
| 107 private: |
| 108 const base::FilePath file_path_; |
| 109 int64_t original_size_; |
| 110 base::Time last_modified_; |
| 111 bool is_directory_; |
| 112 bool is_unsafe_; |
| 113 DISALLOW_COPY_AND_ASSIGN(EntryInfo); |
| 114 }; |
| 115 |
| 116 ZipReader(); |
| 117 ~ZipReader(); |
| 118 |
| 119 // Opens the zip file specified by |zip_file_path|. Returns true on |
| 120 // success. |
| 121 bool Open(const base::FilePath& zip_file_path); |
| 122 |
| 123 // Opens the zip file referred to by the platform file |zip_fd|, without |
| 124 // taking ownership of |zip_fd|. Returns true on success. |
| 125 bool OpenFromPlatformFile(base::PlatformFile zip_fd); |
| 126 |
| 127 // Opens the zip data stored in |data|. This class uses a weak reference to |
| 128 // the given sring while extracting files, i.e. the caller should keep the |
| 129 // string until it finishes extracting files. |
| 130 bool OpenFromString(const std::string& data); |
| 131 |
| 132 // Closes the currently opened zip file. This function is called in the |
| 133 // destructor of the class, so you usually don't need to call this. |
| 134 void Close(); |
| 135 |
| 136 // Returns true if there is at least one entry to read. This function is |
| 137 // used to scan entries with AdvanceToNextEntry(), like: |
| 138 // |
| 139 // while (reader.HasMore()) { |
| 140 // // Do something with the current file here. |
| 141 // reader.AdvanceToNextEntry(); |
| 142 // } |
| 143 bool HasMore(); |
| 144 |
| 145 // Advances the next entry. Returns true on success. |
| 146 bool AdvanceToNextEntry(); |
| 147 |
| 148 // Opens the current entry in the zip file. On success, returns true and |
| 149 // updates the the current entry state (i.e. current_entry_info() is |
| 150 // updated). This function should be called before operations over the |
| 151 // current entry like ExtractCurrentEntryToFile(). |
| 152 // |
| 153 // Note that there is no CloseCurrentEntryInZip(). The the current entry |
| 154 // state is reset automatically as needed. |
| 155 bool OpenCurrentEntryInZip(); |
| 156 |
| 157 // Locates an entry in the zip file and opens it. Returns true on |
| 158 // success. This function internally calls OpenCurrentEntryInZip() on |
| 159 // success. On failure, current_entry_info() becomes NULL. |
| 160 bool LocateAndOpenEntry(const base::FilePath& path_in_zip); |
| 161 |
| 162 // Extracts the current entry in chunks to |delegate|. |
| 163 bool ExtractCurrentEntry(WriterDelegate* delegate) const; |
| 164 |
| 165 // Extracts the current entry to the given output file path. If the |
| 166 // current file is a directory, just creates a directory |
| 167 // instead. Returns true on success. OpenCurrentEntryInZip() must be |
| 168 // called beforehand. |
| 169 // |
| 170 // This function preserves the timestamp of the original entry. If that |
| 171 // timestamp is not valid, the timestamp will be set to the current time. |
| 172 bool ExtractCurrentEntryToFilePath( |
| 173 const base::FilePath& output_file_path) const; |
| 174 |
| 175 // Asynchronously extracts the current entry to the given output file path. |
| 176 // If the current entry is a directory it just creates the directory |
| 177 // synchronously instead. OpenCurrentEntryInZip() must be called beforehand. |
| 178 // success_callback will be called on success and failure_callback will be |
| 179 // called on failure. progress_callback will be called at least once. |
| 180 // Callbacks will be posted to the current MessageLoop in-order. |
| 181 void ExtractCurrentEntryToFilePathAsync( |
| 182 const base::FilePath& output_file_path, |
| 183 const SuccessCallback& success_callback, |
| 184 const FailureCallback& failure_callback, |
| 185 const ProgressCallback& progress_callback); |
| 186 |
| 187 // Extracts the current entry to the given output directory path using |
| 188 // ExtractCurrentEntryToFilePath(). Sub directories are created as needed |
| 189 // based on the file path of the current entry. For example, if the file |
| 190 // path in zip is "foo/bar.txt", and the output directory is "output", |
| 191 // "output/foo/bar.txt" will be created. |
| 192 // |
| 193 // Returns true on success. OpenCurrentEntryInZip() must be called |
| 194 // beforehand. |
| 195 // |
| 196 // This function preserves the timestamp of the original entry. If that |
| 197 // timestamp is not valid, the timestamp will be set to the current time. |
| 198 bool ExtractCurrentEntryIntoDirectory( |
| 199 const base::FilePath& output_directory_path) const; |
| 200 |
| 201 // Extracts the current entry by writing directly to a platform file. |
| 202 // Does not close the file. Returns true on success. |
| 203 bool ExtractCurrentEntryToFile(base::File* file) const; |
| 204 |
| 205 // Extracts the current entry into memory. If the current entry is a directory |
| 206 // the |output| parameter is set to the empty string. If the current entry is |
| 207 // a file, the |output| parameter is filled with its contents. Returns true on |
| 208 // success. OpenCurrentEntryInZip() must be called beforehand. |
| 209 // Note: the |output| parameter can be filled with a big amount of data, avoid |
| 210 // passing it around by value, but by reference or pointer. |
| 211 // Note: the value returned by EntryInfo::original_size() cannot be |
| 212 // trusted, so the real size of the uncompressed contents can be different. |
| 213 // Use max_read_bytes to limit the ammount of memory used to carry the entry. |
| 214 // If the real size of the uncompressed data is bigger than max_read_bytes |
| 215 // then false is returned. |max_read_bytes| must be non-zero. |
| 216 bool ExtractCurrentEntryToString( |
| 217 size_t max_read_bytes, |
| 218 std::string* output) const; |
| 219 |
| 220 // Returns the current entry info. Returns NULL if the current entry is |
| 221 // not yet opened. OpenCurrentEntryInZip() must be called beforehand. |
| 222 EntryInfo* current_entry_info() const { |
| 223 return current_entry_info_.get(); |
| 224 } |
| 225 |
| 226 // Returns the number of entries in the zip file. |
| 227 // Open() must be called beforehand. |
| 228 int num_entries() const { return num_entries_; } |
| 229 |
| 230 private: |
| 231 // Common code used both in Open and OpenFromFd. |
| 232 bool OpenInternal(); |
| 233 |
| 234 // Resets the internal state. |
| 235 void Reset(); |
| 236 |
| 237 // Extracts a chunk of the file to the target. Will post a task for the next |
| 238 // chunk and success/failure/progress callbacks as necessary. |
| 239 void ExtractChunk(base::File target_file, |
| 240 const SuccessCallback& success_callback, |
| 241 const FailureCallback& failure_callback, |
| 242 const ProgressCallback& progress_callback, |
| 243 const int64_t offset); |
| 244 |
| 245 unzFile zip_file_; |
| 246 int num_entries_; |
| 247 bool reached_end_; |
| 248 std::unique_ptr<EntryInfo> current_entry_info_; |
| 249 |
| 250 base::WeakPtrFactory<ZipReader> weak_ptr_factory_; |
| 251 |
| 252 DISALLOW_COPY_AND_ASSIGN(ZipReader); |
| 253 }; |
| 254 |
| 255 // A writer delegate that writes to a given File. |
| 256 class FileWriterDelegate : public WriterDelegate { |
| 257 public: |
| 258 explicit FileWriterDelegate(base::File* file); |
| 259 |
| 260 // Truncates the file to the number of bytes written. |
| 261 ~FileWriterDelegate() override; |
| 262 |
| 263 // WriterDelegate methods: |
| 264 |
| 265 // Seeks to the beginning of the file, returning false if the seek fails. |
| 266 bool PrepareOutput() override; |
| 267 |
| 268 // Writes |num_bytes| bytes of |data| to the file, returning false on error or |
| 269 // if not all bytes could be written. |
| 270 bool WriteBytes(const char* data, int num_bytes) override; |
| 271 |
| 272 private: |
| 273 base::File* file_; |
| 274 int64_t file_length_; |
| 275 |
| 276 DISALLOW_COPY_AND_ASSIGN(FileWriterDelegate); |
| 277 }; |
| 278 |
| 279 } // namespace zip |
| 280 |
| 281 #endif // THIRD_PARTY_ZLIB_GOOGLE_ZIP_READER_H_ |
OLD | NEW |