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 | |
41 // This class is used for reading zip files. A typical use case of this | 26 // This class is used for reading zip files. A typical use case of this |
42 // class is to scan entries in a zip file and extract them. The code will | 27 // class is to scan entries in a zip file and extract them. The code will |
43 // look like: | 28 // look like: |
44 // | 29 // |
45 // ZipReader reader; | 30 // ZipReader reader; |
46 // reader.Open(zip_file_path); | 31 // reader.Open(zip_file_path); |
47 // while (reader.HasMore()) { | 32 // while (reader.HasMore()) { |
48 // reader.OpenCurrentEntryInZip(); | 33 // reader.OpenCurrentEntryInZip(); |
49 // reader.ExtractCurrentEntryToDirectory(output_directory_path); | 34 // reader.ExtractCurrentEntryToDirectory(output_directory_path); |
50 // reader.AdvanceToNextEntry(); | 35 // reader.AdvanceToNextEntry(); |
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
149 // | 134 // |
150 // Note that there is no CloseCurrentEntryInZip(). The the current entry | 135 // Note that there is no CloseCurrentEntryInZip(). The the current entry |
151 // state is reset automatically as needed. | 136 // state is reset automatically as needed. |
152 bool OpenCurrentEntryInZip(); | 137 bool OpenCurrentEntryInZip(); |
153 | 138 |
154 // Locates an entry in the zip file and opens it. Returns true on | 139 // Locates an entry in the zip file and opens it. Returns true on |
155 // success. This function internally calls OpenCurrentEntryInZip() on | 140 // success. This function internally calls OpenCurrentEntryInZip() on |
156 // success. On failure, current_entry_info() becomes NULL. | 141 // success. On failure, current_entry_info() becomes NULL. |
157 bool LocateAndOpenEntry(const base::FilePath& path_in_zip); | 142 bool LocateAndOpenEntry(const base::FilePath& path_in_zip); |
158 | 143 |
159 // Extracts the current entry in chunks to |delegate|. | |
160 bool ExtractCurrentEntry(WriterDelegate* delegate) const; | |
161 | |
162 // Extracts the current entry to the given output file path. If the | 144 // Extracts the current entry to the given output file path. If the |
163 // current file is a directory, just creates a directory | 145 // current file is a directory, just creates a directory |
164 // instead. Returns true on success. OpenCurrentEntryInZip() must be | 146 // instead. Returns true on success. OpenCurrentEntryInZip() must be |
165 // called beforehand. | 147 // called beforehand. |
166 // | 148 // |
167 // This function preserves the timestamp of the original entry. If that | 149 // This function preserves the timestamp of the original entry. If that |
168 // timestamp is not valid, the timestamp will be set to the current time. | 150 // timestamp is not valid, the timestamp will be set to the current time. |
169 bool ExtractCurrentEntryToFilePath( | 151 bool ExtractCurrentEntryToFilePath(const base::FilePath& output_file_path); |
170 const base::FilePath& output_file_path) const; | |
171 | 152 |
172 // Asynchronously extracts the current entry to the given output file path. | 153 // Asynchronously extracts the current entry to the given output file path. |
173 // If the current entry is a directory it just creates the directory | 154 // If the current entry is a directory it just creates the directory |
174 // synchronously instead. OpenCurrentEntryInZip() must be called beforehand. | 155 // synchronously instead. OpenCurrentEntryInZip() must be called beforehand. |
175 // success_callback will be called on success and failure_callback will be | 156 // success_callback will be called on success and failure_callback will be |
176 // called on failure. progress_callback will be called at least once. | 157 // called on failure. progress_callback will be called at least once. |
177 // Callbacks will be posted to the current MessageLoop in-order. | 158 // Callbacks will be posted to the current MessageLoop in-order. |
178 void ExtractCurrentEntryToFilePathAsync( | 159 void ExtractCurrentEntryToFilePathAsync( |
179 const base::FilePath& output_file_path, | 160 const base::FilePath& output_file_path, |
180 const SuccessCallback& success_callback, | 161 const SuccessCallback& success_callback, |
181 const FailureCallback& failure_callback, | 162 const FailureCallback& failure_callback, |
182 const ProgressCallback& progress_callback); | 163 const ProgressCallback& progress_callback); |
183 | 164 |
184 // Extracts the current entry to the given output directory path using | 165 // Extracts the current entry to the given output directory path using |
185 // ExtractCurrentEntryToFilePath(). Sub directories are created as needed | 166 // ExtractCurrentEntryToFilePath(). Sub directories are created as needed |
186 // based on the file path of the current entry. For example, if the file | 167 // based on the file path of the current entry. For example, if the file |
187 // path in zip is "foo/bar.txt", and the output directory is "output", | 168 // path in zip is "foo/bar.txt", and the output directory is "output", |
188 // "output/foo/bar.txt" will be created. | 169 // "output/foo/bar.txt" will be created. |
189 // | 170 // |
190 // Returns true on success. OpenCurrentEntryInZip() must be called | 171 // Returns true on success. OpenCurrentEntryInZip() must be called |
191 // beforehand. | 172 // beforehand. |
192 // | 173 // |
193 // This function preserves the timestamp of the original entry. If that | 174 // This function preserves the timestamp of the original entry. If that |
194 // timestamp is not valid, the timestamp will be set to the current time. | 175 // timestamp is not valid, the timestamp will be set to the current time. |
195 bool ExtractCurrentEntryIntoDirectory( | 176 bool ExtractCurrentEntryIntoDirectory( |
196 const base::FilePath& output_directory_path) const; | 177 const base::FilePath& output_directory_path); |
197 | 178 |
198 // Extracts the current entry by writing directly to a platform file. | 179 #if defined(OS_POSIX) |
199 // Does not close the file. Returns true on success. | 180 // Extracts the current entry by writing directly to a file descriptor. |
200 bool ExtractCurrentEntryToFile(base::File* file) const; | 181 // Does not close the file descriptor. Returns true on success. |
| 182 bool ExtractCurrentEntryToFd(int fd); |
| 183 #endif |
201 | 184 |
202 // Extracts the current entry into memory. If the current entry is a directory | 185 // Extracts the current entry into memory. If the current entry is a directory |
203 // the |output| parameter is set to the empty string. If the current entry is | 186 // the |output| parameter is set to the empty string. If the current entry is |
204 // a file, the |output| parameter is filled with its contents. Returns true on | 187 // a file, the |output| parameter is filled with its contents. Returns true on |
205 // success. OpenCurrentEntryInZip() must be called beforehand. | 188 // success. OpenCurrentEntryInZip() must be called beforehand. |
206 // Note: the |output| parameter can be filled with a big amount of data, avoid | 189 // Note: the |output| parameter can be filled with a big amount of data, avoid |
207 // passing it around by value, but by reference or pointer. | 190 // passing it around by value, but by reference or pointer. |
208 // Note: the value returned by EntryInfo::original_size() cannot be | 191 // Note: the value returned by EntryInfo::original_size() cannot be |
209 // trusted, so the real size of the uncompressed contents can be different. | 192 // trusted, so the real size of the uncompressed contents can be different. |
210 // Use max_read_bytes to limit the ammount of memory used to carry the entry. | 193 // 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... |
242 unzFile zip_file_; | 225 unzFile zip_file_; |
243 int num_entries_; | 226 int num_entries_; |
244 bool reached_end_; | 227 bool reached_end_; |
245 scoped_ptr<EntryInfo> current_entry_info_; | 228 scoped_ptr<EntryInfo> current_entry_info_; |
246 | 229 |
247 base::WeakPtrFactory<ZipReader> weak_ptr_factory_; | 230 base::WeakPtrFactory<ZipReader> weak_ptr_factory_; |
248 | 231 |
249 DISALLOW_COPY_AND_ASSIGN(ZipReader); | 232 DISALLOW_COPY_AND_ASSIGN(ZipReader); |
250 }; | 233 }; |
251 | 234 |
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 | |
276 } // namespace zip | 235 } // namespace zip |
277 | 236 |
278 #endif // THIRD_PARTY_ZLIB_GOOGLE_ZIP_READER_H_ | 237 #endif // THIRD_PARTY_ZLIB_GOOGLE_ZIP_READER_H_ |
OLD | NEW |