| 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 | |
| 5 #ifndef THIRD_PARTY_ZLIB_GOOGLE_ZIP_READER_H_ | 4 #ifndef THIRD_PARTY_ZLIB_GOOGLE_ZIP_READER_H_ |
| 6 #define THIRD_PARTY_ZLIB_GOOGLE_ZIP_READER_H_ | 5 #define THIRD_PARTY_ZLIB_GOOGLE_ZIP_READER_H_ |
| 7 | 6 |
| 8 #include <string> | 7 #include <string> |
| 9 | 8 |
| 10 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
| 10 #include "base/callback.h" |
| 11 #include "base/file_util.h" | 11 #include "base/file_util.h" |
| 12 #include "base/files/file_path.h" | 12 #include "base/files/file_path.h" |
| 13 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
| 14 #include "base/memory/weak_ptr.h" |
| 14 #include "base/platform_file.h" | 15 #include "base/platform_file.h" |
| 15 #include "base/time/time.h" | 16 #include "base/time/time.h" |
| 16 | 17 |
| 17 #if defined(USE_SYSTEM_MINIZIP) | 18 #if defined(USE_SYSTEM_MINIZIP) |
| 18 #include <minizip/unzip.h> | 19 #include <minizip/unzip.h> |
| 19 #else | 20 #else |
| 20 #include "third_party/zlib/contrib/minizip/unzip.h" | 21 #include "third_party/zlib/contrib/minizip/unzip.h" |
| 21 #endif | 22 #endif |
| 22 | 23 |
| 23 namespace zip { | 24 namespace zip { |
| (...skipping 11 matching lines...) Expand all Loading... |
| 35 // } | 36 // } |
| 36 // | 37 // |
| 37 // For simplicty, error checking is omitted in the example code above. The | 38 // For simplicty, error checking is omitted in the example code above. The |
| 38 // production code should check return values from all of these functions. | 39 // production code should check return values from all of these functions. |
| 39 // | 40 // |
| 40 // This calls can also be used for random access of contents in a zip file | 41 // This calls can also be used for random access of contents in a zip file |
| 41 // using LocateAndOpenEntry(). | 42 // using LocateAndOpenEntry(). |
| 42 // | 43 // |
| 43 class ZipReader { | 44 class ZipReader { |
| 44 public: | 45 public: |
| 46 // A callback that is called when the operation is successful. |
| 47 typedef base::Closure SuccessCallback; |
| 48 // A callback that is called when the operation fails. |
| 49 typedef base::Closure FailureCallback; |
| 50 // A callback that is called periodically during the operation with the number |
| 51 // of bytes that have been processed so far. |
| 52 typedef base::Callback<void(int64)> ProgressCallback; |
| 53 |
| 45 // This class represents information of an entry (file or directory) in | 54 // This class represents information of an entry (file or directory) in |
| 46 // a zip file. | 55 // a zip file. |
| 47 class EntryInfo { | 56 class EntryInfo { |
| 48 public: | 57 public: |
| 49 EntryInfo(const std::string& filename_in_zip, | 58 EntryInfo(const std::string& filename_in_zip, |
| 50 const unz_file_info& raw_file_info); | 59 const unz_file_info& raw_file_info); |
| 51 | 60 |
| 52 // Returns the file path. The path is usually relative like | 61 // Returns the file path. The path is usually relative like |
| 53 // "foo/bar.txt", but if it's absolute, is_unsafe() returns true. | 62 // "foo/bar.txt", but if it's absolute, is_unsafe() returns true. |
| 54 const base::FilePath& file_path() const { return file_path_; } | 63 const base::FilePath& file_path() const { return file_path_; } |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 132 | 141 |
| 133 // Extracts the current entry to the given output file path. If the | 142 // Extracts the current entry to the given output file path. If the |
| 134 // current file is a directory, just creates a directory | 143 // current file is a directory, just creates a directory |
| 135 // instead. Returns true on success. OpenCurrentEntryInZip() must be | 144 // instead. Returns true on success. OpenCurrentEntryInZip() must be |
| 136 // called beforehand. | 145 // called beforehand. |
| 137 // | 146 // |
| 138 // This function preserves the timestamp of the original entry. If that | 147 // This function preserves the timestamp of the original entry. If that |
| 139 // timestamp is not valid, the timestamp will be set to the current time. | 148 // timestamp is not valid, the timestamp will be set to the current time. |
| 140 bool ExtractCurrentEntryToFilePath(const base::FilePath& output_file_path); | 149 bool ExtractCurrentEntryToFilePath(const base::FilePath& output_file_path); |
| 141 | 150 |
| 151 // Asynchronously extracts the current entry to the given output file path. |
| 152 // If the current entry is a directory it just creates the directory |
| 153 // synchronously instead. OpenCurrentEntryInZip() must be called beforehand. |
| 154 // success_callback will be called on success and failure_callback will be |
| 155 // called on failure. progress_callback will be called at least once. |
| 156 // Callbacks will be posted to the current MessageLoop in-order. |
| 157 void ExtractCurrentEntryToFilePathAsync( |
| 158 const base::FilePath& output_file_path, |
| 159 const SuccessCallback& success_callback, |
| 160 const FailureCallback& failure_callback, |
| 161 const ProgressCallback& progress_callback); |
| 162 |
| 142 // Extracts the current entry to the given output directory path using | 163 // Extracts the current entry to the given output directory path using |
| 143 // ExtractCurrentEntryToFilePath(). Sub directories are created as needed | 164 // ExtractCurrentEntryToFilePath(). Sub directories are created as needed |
| 144 // based on the file path of the current entry. For example, if the file | 165 // based on the file path of the current entry. For example, if the file |
| 145 // path in zip is "foo/bar.txt", and the output directory is "output", | 166 // path in zip is "foo/bar.txt", and the output directory is "output", |
| 146 // "output/foo/bar.txt" will be created. | 167 // "output/foo/bar.txt" will be created. |
| 147 // | 168 // |
| 148 // Returns true on success. OpenCurrentEntryInZip() must be called | 169 // Returns true on success. OpenCurrentEntryInZip() must be called |
| 149 // beforehand. | 170 // beforehand. |
| 150 // | 171 // |
| 151 // This function preserves the timestamp of the original entry. If that | 172 // This function preserves the timestamp of the original entry. If that |
| (...skipping 17 matching lines...) Expand all Loading... |
| 169 // Open() must be called beforehand. | 190 // Open() must be called beforehand. |
| 170 int num_entries() const { return num_entries_; } | 191 int num_entries() const { return num_entries_; } |
| 171 | 192 |
| 172 private: | 193 private: |
| 173 // Common code used both in Open and OpenFromFd. | 194 // Common code used both in Open and OpenFromFd. |
| 174 bool OpenInternal(); | 195 bool OpenInternal(); |
| 175 | 196 |
| 176 // Resets the internal state. | 197 // Resets the internal state. |
| 177 void Reset(); | 198 void Reset(); |
| 178 | 199 |
| 200 // Extracts a chunk of the file to the target. Will post a task for the next |
| 201 // chunk and success/failure/progress callbacks as necessary. |
| 202 void ExtractChunk(base::PlatformFile target_file, |
| 203 const SuccessCallback& success_callback, |
| 204 const FailureCallback& failure_callback, |
| 205 const ProgressCallback& progress_callback, |
| 206 const int64 offset); |
| 207 |
| 179 unzFile zip_file_; | 208 unzFile zip_file_; |
| 180 int num_entries_; | 209 int num_entries_; |
| 181 bool reached_end_; | 210 bool reached_end_; |
| 182 scoped_ptr<EntryInfo> current_entry_info_; | 211 scoped_ptr<EntryInfo> current_entry_info_; |
| 183 | 212 |
| 213 base::WeakPtrFactory<ZipReader> weak_ptr_factory_; |
| 214 |
| 184 DISALLOW_COPY_AND_ASSIGN(ZipReader); | 215 DISALLOW_COPY_AND_ASSIGN(ZipReader); |
| 185 }; | 216 }; |
| 186 | 217 |
| 187 } // namespace zip | 218 } // namespace zip |
| 188 | 219 |
| 189 #endif // THIRD_PARTY_ZLIB_GOOGLE_ZIP_READER_H_ | 220 #endif // THIRD_PARTY_ZLIB_GOOGLE_ZIP_READER_H_ |
| OLD | NEW |