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_
9 #include "base/basictypes.h"
10 #include "base/callback.h"
11 #include "base/files/file.h"
12 #include "base/files/file_path.h"
13 #include "base/files/file_util.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/memory/weak_ptr.h"
16 #include "base/time/time.h"
18 #if defined(USE_SYSTEM_MINIZIP)
19 #include <minizip/unzip.h>
21 #include "third_party/zlib/contrib/minizip/unzip.h"
26 // A delegate interface used to stream out an entry; see
27 // ZipReader::ExtractCurrentEntry.
28 class WriterDelegate
{
30 virtual ~WriterDelegate() {}
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;
36 // Invoked to write the next chunk of data. Return false on failure to cancel
38 virtual bool WriteBytes(const char* data
, int num_bytes
) = 0;
41 // 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
46 // reader.Open(zip_file_path);
47 // while (reader.HasMore()) {
48 // reader.OpenCurrentEntryInZip();
49 // reader.ExtractCurrentEntryToDirectory(output_directory_path);
50 // reader.AdvanceToNextEntry();
53 // For simplicity, error checking is omitted in the example code above. The
54 // production code should check return values from all of these functions.
56 // This calls can also be used for random access of contents in a zip file
57 // using LocateAndOpenEntry().
61 // A callback that is called when the operation is successful.
62 typedef base::Closure SuccessCallback
;
63 // A callback that is called when the operation fails.
64 typedef base::Closure FailureCallback
;
65 // A callback that is called periodically during the operation with the number
66 // of bytes that have been processed so far.
67 typedef base::Callback
<void(int64
)> ProgressCallback
;
69 // This class represents information of an entry (file or directory) in
73 EntryInfo(const std::string
& filename_in_zip
,
74 const unz_file_info
& raw_file_info
);
76 // Returns the file path. The path is usually relative like
77 // "foo/bar.txt", but if it's absolute, is_unsafe() returns true.
78 const base::FilePath
& file_path() const { return file_path_
; }
80 // Returns the size of the original file (i.e. after uncompressed).
81 // Returns 0 if the entry is a directory.
82 // Note: this value should not be trusted, because it is stored as metadata
83 // in the zip archive and can be different from the real uncompressed size.
84 int64
original_size() const { return original_size_
; }
86 // Returns the last modified time. If the time stored in the zip file was
87 // not valid, the unix epoch will be returned.
89 // The time stored in the zip archive uses the MS-DOS date and time format.
90 // http://msdn.microsoft.com/en-us/library/ms724247(v=vs.85).aspx
91 // As such the following limitations apply:
92 // * only years from 1980 to 2107 can be represented.
93 // * the time stamp has a 2 second resolution.
94 // * there's no timezone information, so the time is interpreted as local.
95 base::Time
last_modified() const { return last_modified_
; }
97 // Returns true if the entry is a directory.
98 bool is_directory() const { return is_directory_
; }
100 // Returns true if the entry is unsafe, like having ".." or invalid
101 // UTF-8 characters in its file name, or the file path is absolute.
102 bool is_unsafe() const { return is_unsafe_
; }
105 const base::FilePath file_path_
;
106 int64 original_size_
;
107 base::Time last_modified_
;
110 DISALLOW_COPY_AND_ASSIGN(EntryInfo
);
116 // Opens the zip file specified by |zip_file_path|. Returns true on
118 bool Open(const base::FilePath
& zip_file_path
);
120 // Opens the zip file referred to by the platform file |zip_fd|, without
121 // taking ownership of |zip_fd|. Returns true on success.
122 bool OpenFromPlatformFile(base::PlatformFile zip_fd
);
124 // Opens the zip data stored in |data|. This class uses a weak reference to
125 // the given sring while extracting files, i.e. the caller should keep the
126 // string until it finishes extracting files.
127 bool OpenFromString(const std::string
& data
);
129 // Closes the currently opened zip file. This function is called in the
130 // destructor of the class, so you usually don't need to call this.
133 // Returns true if there is at least one entry to read. This function is
134 // used to scan entries with AdvanceToNextEntry(), like:
136 // while (reader.HasMore()) {
137 // // Do something with the current file here.
138 // reader.AdvanceToNextEntry();
142 // Advances the next entry. Returns true on success.
143 bool AdvanceToNextEntry();
145 // Opens the current entry in the zip file. On success, returns true and
146 // updates the the current entry state (i.e. current_entry_info() is
147 // updated). This function should be called before operations over the
148 // current entry like ExtractCurrentEntryToFile().
150 // Note that there is no CloseCurrentEntryInZip(). The the current entry
151 // state is reset automatically as needed.
152 bool OpenCurrentEntryInZip();
154 // Locates an entry in the zip file and opens it. Returns true on
155 // success. This function internally calls OpenCurrentEntryInZip() on
156 // success. On failure, current_entry_info() becomes NULL.
157 bool LocateAndOpenEntry(const base::FilePath
& path_in_zip
);
159 // Extracts the current entry in chunks to |delegate|.
160 bool ExtractCurrentEntry(WriterDelegate
* delegate
) const;
162 // Extracts the current entry to the given output file path. If the
163 // current file is a directory, just creates a directory
164 // instead. Returns true on success. OpenCurrentEntryInZip() must be
165 // called beforehand.
167 // 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.
169 bool ExtractCurrentEntryToFilePath(
170 const base::FilePath
& output_file_path
) const;
172 // Asynchronously extracts the current entry to the given output file path.
173 // If the current entry is a directory it just creates the directory
174 // synchronously instead. OpenCurrentEntryInZip() must be called beforehand.
175 // success_callback will be called on success and failure_callback will be
176 // called on failure. progress_callback will be called at least once.
177 // Callbacks will be posted to the current MessageLoop in-order.
178 void ExtractCurrentEntryToFilePathAsync(
179 const base::FilePath
& output_file_path
,
180 const SuccessCallback
& success_callback
,
181 const FailureCallback
& failure_callback
,
182 const ProgressCallback
& progress_callback
);
184 // Extracts the current entry to the given output directory path using
185 // ExtractCurrentEntryToFilePath(). Sub directories are created as needed
186 // 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",
188 // "output/foo/bar.txt" will be created.
190 // Returns true on success. OpenCurrentEntryInZip() must be called
193 // 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.
195 bool ExtractCurrentEntryIntoDirectory(
196 const base::FilePath
& output_directory_path
) const;
198 // Extracts the current entry by writing directly to a platform file.
199 // Does not close the file. Returns true on success.
200 bool ExtractCurrentEntryToFile(base::File
* file
) const;
202 // 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
204 // a file, the |output| parameter is filled with its contents. Returns true on
205 // success. OpenCurrentEntryInZip() must be called beforehand.
206 // 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.
208 // Note: the value returned by EntryInfo::original_size() cannot be
209 // 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.
211 // If the real size of the uncompressed data is bigger than max_read_bytes
212 // then false is returned. |max_read_bytes| must be non-zero.
213 bool ExtractCurrentEntryToString(
214 size_t max_read_bytes
,
215 std::string
* output
) const;
217 // Returns the current entry info. Returns NULL if the current entry is
218 // not yet opened. OpenCurrentEntryInZip() must be called beforehand.
219 EntryInfo
* current_entry_info() const {
220 return current_entry_info_
.get();
223 // Returns the number of entries in the zip file.
224 // Open() must be called beforehand.
225 int num_entries() const { return num_entries_
; }
228 // Common code used both in Open and OpenFromFd.
231 // Resets the internal state.
234 // Extracts a chunk of the file to the target. Will post a task for the next
235 // chunk and success/failure/progress callbacks as necessary.
236 void ExtractChunk(base::File target_file
,
237 const SuccessCallback
& success_callback
,
238 const FailureCallback
& failure_callback
,
239 const ProgressCallback
& progress_callback
,
245 scoped_ptr
<EntryInfo
> current_entry_info_
;
247 base::WeakPtrFactory
<ZipReader
> weak_ptr_factory_
;
249 DISALLOW_COPY_AND_ASSIGN(ZipReader
);
252 // A writer delegate that writes to a given File.
253 class FileWriterDelegate
: public WriterDelegate
{
255 explicit FileWriterDelegate(base::File
* file
);
257 // Truncates the file to the number of bytes written.
258 ~FileWriterDelegate() override
;
260 // WriterDelegate methods:
262 // Seeks to the beginning of the file, returning false if the seek fails.
263 bool PrepareOutput() override
;
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
;
271 int64_t file_length_
;
273 DISALLOW_COPY_AND_ASSIGN(FileWriterDelegate
);
278 #endif // THIRD_PARTY_ZLIB_GOOGLE_ZIP_READER_H_