| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 #ifndef THIRD_PARTY_ZLIB_GOOGLE_ZIP_READER_H_ | 4 #ifndef THIRD_PARTY_ZLIB_GOOGLE_ZIP_READER_H_ |
| 5 #define THIRD_PARTY_ZLIB_GOOGLE_ZIP_READER_H_ | 5 #define THIRD_PARTY_ZLIB_GOOGLE_ZIP_READER_H_ |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
| 10 #include "base/callback.h" | 10 #include "base/callback.h" |
| 11 #include "base/files/file.h" | 11 #include "base/files/file.h" |
| 12 #include "base/files/file_path.h" | 12 #include "base/files/file_path.h" |
| 13 #include "base/files/file_util.h" | 13 #include "base/files/file_util.h" |
| 14 #include "base/memory/scoped_ptr.h" | 14 #include "base/memory/scoped_ptr.h" |
| 15 #include "base/memory/weak_ptr.h" | 15 #include "base/memory/weak_ptr.h" |
| 16 #include "base/time/time.h" | 16 #include "base/time/time.h" |
| 17 | 17 |
| 18 #if defined(USE_SYSTEM_MINIZIP) | 18 #if defined(USE_SYSTEM_MINIZIP) |
| 19 #include <minizip/unzip.h> | 19 #include <minizip/unzip.h> |
| 20 #else | 20 #else |
| 21 #include "third_party/zlib/contrib/minizip/unzip.h" | 21 #include "third_party/zlib/contrib/minizip/unzip.h" |
| 22 #endif | 22 #endif |
| 23 | 23 |
| 24 namespace zip { | 24 namespace zip { |
| 25 | 25 |
| 26 // A delegate interface used to stream out an entry; see |
| 27 // ZipReader::ExtractCurrentEntry. |
| 28 class WriterDelegate { |
| 29 public: |
| 30 virtual ~WriterDelegate() {} |
| 31 |
| 32 // Invoked once before any data is streamed out to pave the way (e.g., to open |
| 33 // the output file). Return false on failure to cancel extraction. |
| 34 virtual bool PrepareOutput() = 0; |
| 35 |
| 36 // Invoked to write the next chunk of data. Return false on failure to cancel |
| 37 // extraction. |
| 38 virtual bool WriteBytes(const char* data, int num_bytes) = 0; |
| 39 }; |
| 40 |
| 26 // This class is used for reading zip files. A typical use case of this | 41 // This class is used for reading zip files. A typical use case of this |
| 27 // class is to scan entries in a zip file and extract them. The code will | 42 // class is to scan entries in a zip file and extract them. The code will |
| 28 // look like: | 43 // look like: |
| 29 // | 44 // |
| 30 // ZipReader reader; | 45 // ZipReader reader; |
| 31 // reader.Open(zip_file_path); | 46 // reader.Open(zip_file_path); |
| 32 // while (reader.HasMore()) { | 47 // while (reader.HasMore()) { |
| 33 // reader.OpenCurrentEntryInZip(); | 48 // reader.OpenCurrentEntryInZip(); |
| 34 // reader.ExtractCurrentEntryToDirectory(output_directory_path); | 49 // reader.ExtractCurrentEntryToDirectory(output_directory_path); |
| 35 // reader.AdvanceToNextEntry(); | 50 // reader.AdvanceToNextEntry(); |
| (...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 134 // | 149 // |
| 135 // Note that there is no CloseCurrentEntryInZip(). The the current entry | 150 // Note that there is no CloseCurrentEntryInZip(). The the current entry |
| 136 // state is reset automatically as needed. | 151 // state is reset automatically as needed. |
| 137 bool OpenCurrentEntryInZip(); | 152 bool OpenCurrentEntryInZip(); |
| 138 | 153 |
| 139 // Locates an entry in the zip file and opens it. Returns true on | 154 // Locates an entry in the zip file and opens it. Returns true on |
| 140 // success. This function internally calls OpenCurrentEntryInZip() on | 155 // success. This function internally calls OpenCurrentEntryInZip() on |
| 141 // success. On failure, current_entry_info() becomes NULL. | 156 // success. On failure, current_entry_info() becomes NULL. |
| 142 bool LocateAndOpenEntry(const base::FilePath& path_in_zip); | 157 bool LocateAndOpenEntry(const base::FilePath& path_in_zip); |
| 143 | 158 |
| 159 // Extracts the current entry in chunks to |delegate|. |
| 160 bool ExtractCurrentEntry(WriterDelegate* delegate) const; |
| 161 |
| 144 // Extracts the current entry to the given output file path. If the | 162 // Extracts the current entry to the given output file path. If the |
| 145 // current file is a directory, just creates a directory | 163 // current file is a directory, just creates a directory |
| 146 // instead. Returns true on success. OpenCurrentEntryInZip() must be | 164 // instead. Returns true on success. OpenCurrentEntryInZip() must be |
| 147 // called beforehand. | 165 // called beforehand. |
| 148 // | 166 // |
| 149 // This function preserves the timestamp of the original entry. If that | 167 // This function preserves the timestamp of the original entry. If that |
| 150 // timestamp is not valid, the timestamp will be set to the current time. | 168 // timestamp is not valid, the timestamp will be set to the current time. |
| 151 bool ExtractCurrentEntryToFilePath(const base::FilePath& output_file_path); | 169 bool ExtractCurrentEntryToFilePath( |
| 170 const base::FilePath& output_file_path) const; |
| 152 | 171 |
| 153 // Asynchronously extracts the current entry to the given output file path. | 172 // Asynchronously extracts the current entry to the given output file path. |
| 154 // If the current entry is a directory it just creates the directory | 173 // If the current entry is a directory it just creates the directory |
| 155 // synchronously instead. OpenCurrentEntryInZip() must be called beforehand. | 174 // synchronously instead. OpenCurrentEntryInZip() must be called beforehand. |
| 156 // success_callback will be called on success and failure_callback will be | 175 // success_callback will be called on success and failure_callback will be |
| 157 // called on failure. progress_callback will be called at least once. | 176 // called on failure. progress_callback will be called at least once. |
| 158 // Callbacks will be posted to the current MessageLoop in-order. | 177 // Callbacks will be posted to the current MessageLoop in-order. |
| 159 void ExtractCurrentEntryToFilePathAsync( | 178 void ExtractCurrentEntryToFilePathAsync( |
| 160 const base::FilePath& output_file_path, | 179 const base::FilePath& output_file_path, |
| 161 const SuccessCallback& success_callback, | 180 const SuccessCallback& success_callback, |
| 162 const FailureCallback& failure_callback, | 181 const FailureCallback& failure_callback, |
| 163 const ProgressCallback& progress_callback); | 182 const ProgressCallback& progress_callback); |
| 164 | 183 |
| 165 // Extracts the current entry to the given output directory path using | 184 // Extracts the current entry to the given output directory path using |
| 166 // ExtractCurrentEntryToFilePath(). Sub directories are created as needed | 185 // ExtractCurrentEntryToFilePath(). Sub directories are created as needed |
| 167 // based on the file path of the current entry. For example, if the file | 186 // based on the file path of the current entry. For example, if the file |
| 168 // path in zip is "foo/bar.txt", and the output directory is "output", | 187 // path in zip is "foo/bar.txt", and the output directory is "output", |
| 169 // "output/foo/bar.txt" will be created. | 188 // "output/foo/bar.txt" will be created. |
| 170 // | 189 // |
| 171 // Returns true on success. OpenCurrentEntryInZip() must be called | 190 // Returns true on success. OpenCurrentEntryInZip() must be called |
| 172 // beforehand. | 191 // beforehand. |
| 173 // | 192 // |
| 174 // This function preserves the timestamp of the original entry. If that | 193 // This function preserves the timestamp of the original entry. If that |
| 175 // timestamp is not valid, the timestamp will be set to the current time. | 194 // timestamp is not valid, the timestamp will be set to the current time. |
| 176 bool ExtractCurrentEntryIntoDirectory( | 195 bool ExtractCurrentEntryIntoDirectory( |
| 177 const base::FilePath& output_directory_path); | 196 const base::FilePath& output_directory_path) const; |
| 178 | 197 |
| 179 #if defined(OS_POSIX) | 198 // Extracts the current entry by writing directly to a platform file. |
| 180 // Extracts the current entry by writing directly to a file descriptor. | 199 // Does not close the file. Returns true on success. |
| 181 // Does not close the file descriptor. Returns true on success. | 200 bool ExtractCurrentEntryToFile(base::File* file) const; |
| 182 bool ExtractCurrentEntryToFd(int fd); | |
| 183 #endif | |
| 184 | 201 |
| 185 // Extracts the current entry into memory. If the current entry is a directory | 202 // Extracts the current entry into memory. If the current entry is a directory |
| 186 // the |output| parameter is set to the empty string. If the current entry is | 203 // the |output| parameter is set to the empty string. If the current entry is |
| 187 // a file, the |output| parameter is filled with its contents. Returns true on | 204 // a file, the |output| parameter is filled with its contents. Returns true on |
| 188 // success. OpenCurrentEntryInZip() must be called beforehand. | 205 // success. OpenCurrentEntryInZip() must be called beforehand. |
| 189 // Note: the |output| parameter can be filled with a big amount of data, avoid | 206 // Note: the |output| parameter can be filled with a big amount of data, avoid |
| 190 // passing it around by value, but by reference or pointer. | 207 // passing it around by value, but by reference or pointer. |
| 191 // Note: the value returned by EntryInfo::original_size() cannot be | 208 // Note: the value returned by EntryInfo::original_size() cannot be |
| 192 // trusted, so the real size of the uncompressed contents can be different. | 209 // trusted, so the real size of the uncompressed contents can be different. |
| 193 // Use max_read_bytes to limit the ammount of memory used to carry the entry. | 210 // Use max_read_bytes to limit the ammount of memory used to carry the entry. |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 225 unzFile zip_file_; | 242 unzFile zip_file_; |
| 226 int num_entries_; | 243 int num_entries_; |
| 227 bool reached_end_; | 244 bool reached_end_; |
| 228 scoped_ptr<EntryInfo> current_entry_info_; | 245 scoped_ptr<EntryInfo> current_entry_info_; |
| 229 | 246 |
| 230 base::WeakPtrFactory<ZipReader> weak_ptr_factory_; | 247 base::WeakPtrFactory<ZipReader> weak_ptr_factory_; |
| 231 | 248 |
| 232 DISALLOW_COPY_AND_ASSIGN(ZipReader); | 249 DISALLOW_COPY_AND_ASSIGN(ZipReader); |
| 233 }; | 250 }; |
| 234 | 251 |
| 252 // A writer delegate that writes to a given File. |
| 253 class FileWriterDelegate : public WriterDelegate { |
| 254 public: |
| 255 explicit FileWriterDelegate(base::File* file); |
| 256 |
| 257 // Truncates the file to the number of bytes written. |
| 258 ~FileWriterDelegate() override; |
| 259 |
| 260 // WriterDelegate methods: |
| 261 |
| 262 // Seeks to the beginning of the file, returning false if the seek fails. |
| 263 bool PrepareOutput() override; |
| 264 |
| 265 // Writes |num_bytes| bytes of |data| to the file, returning false on error or |
| 266 // if not all bytes could be written. |
| 267 bool WriteBytes(const char* data, int num_bytes) override; |
| 268 |
| 269 private: |
| 270 base::File* file_; |
| 271 int64_t file_length_; |
| 272 |
| 273 DISALLOW_COPY_AND_ASSIGN(FileWriterDelegate); |
| 274 }; |
| 275 |
| 235 } // namespace zip | 276 } // namespace zip |
| 236 | 277 |
| 237 #endif // THIRD_PARTY_ZLIB_GOOGLE_ZIP_READER_H_ | 278 #endif // THIRD_PARTY_ZLIB_GOOGLE_ZIP_READER_H_ |
| OLD | NEW |