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.
5 #ifndef THIRD_PARTY_ZLIB_GOOGLE_ZIP_READER_H_
6 #define THIRD_PARTY_ZLIB_GOOGLE_ZIP_READER_H_
10 #include "base/basictypes.h"
11 #include "base/file_util.h"
12 #include "base/files/file_path.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/platform_file.h"
15 #include "base/time/time.h"
17 #if defined(USE_SYSTEM_MINIZIP)
18 #include <minizip/unzip.h>
20 #include "third_party/zlib/contrib/minizip/unzip.h"
25 // This class is used for reading zip files. A typical use case of this
26 // class is to scan entries in a zip file and extract them. The code will
30 // reader.Open(zip_file_path);
31 // while (reader.HasMore()) {
32 // reader.OpenCurrentEntryInZip();
33 // reader.ExtractCurrentEntryToDirectory(output_directory_path);
34 // reader.AdvanceToNextEntry();
37 // For simplicty, error checking is omitted in the example code above. The
38 // production code should check return values from all of these functions.
40 // This calls can also be used for random access of contents in a zip file
41 // using LocateAndOpenEntry().
45 // This class represents information of an entry (file or directory) in
49 EntryInfo(const std::string& filename_in_zip,
50 const unz_file_info& raw_file_info);
52 // Returns the file path. The path is usually relative like
53 // "foo/bar.txt", but if it's absolute, is_unsafe() returns true.
54 const base::FilePath& file_path() const { return file_path_; }
56 // Returns the size of the original file (i.e. after uncompressed).
57 // Returns 0 if the entry is a directory.
58 int64 original_size() const { return original_size_; }
60 // Returns the last modified time.
61 base::Time last_modified() const { return last_modified_; }
63 // Returns true if the entry is a directory.
64 bool is_directory() const { return is_directory_; }
66 // Returns true if the entry is unsafe, like having ".." or invalid
67 // UTF-8 characters in its file name, or the file path is absolute.
68 bool is_unsafe() const { return is_unsafe_; }
71 const base::FilePath file_path_;
73 base::Time last_modified_;
76 DISALLOW_COPY_AND_ASSIGN(EntryInfo);
82 // Opens the zip file specified by |zip_file_path|. Returns true on
84 bool Open(const base::FilePath& zip_file_path);
86 // Opens the zip file referred to by the platform file |zip_fd|.
87 // Returns true on success.
88 bool OpenFromPlatformFile(base::PlatformFile zip_fd);
90 // Opens the zip data stored in |data|. This class uses a weak reference to
91 // the given sring while extracting files, i.e. the caller should keep the
92 // string until it finishes extracting files.
93 bool OpenFromString(const std::string& data);
95 // Closes the currently opened zip file. This function is called in the
96 // destructor of the class, so you usually don't need to call this.
99 // Returns true if there is at least one entry to read. This function is
100 // used to scan entries with AdvanceToNextEntry(), like:
102 // while (reader.HasMore()) {
103 // // Do something with the current file here.
104 // reader.AdvanceToNextEntry();
108 // Advances the next entry. Returns true on success.
109 bool AdvanceToNextEntry();
111 // Opens the current entry in the zip file. On success, returns true and
112 // updates the the current entry state (i.e. current_entry_info() is
113 // updated). This function should be called before operations over the
114 // current entry like ExtractCurrentEntryToFile().
116 // Note that there is no CloseCurrentEntryInZip(). The the current entry
117 // state is reset automatically as needed.
118 bool OpenCurrentEntryInZip();
120 // Locates an entry in the zip file and opens it. Returns true on
121 // success. This function internally calls OpenCurrentEntryInZip() on
122 // success. On failure, current_entry_info() becomes NULL.
123 bool LocateAndOpenEntry(const base::FilePath& path_in_zip);
125 // Extracts the current entry to the given output file path. If the
126 // current file is a directory, just creates a directory
127 // instead. Returns true on success. OpenCurrentEntryInZip() must be
128 // called beforehand.
130 // This function does not preserve the timestamp of the original entry.
131 bool ExtractCurrentEntryToFilePath(const base::FilePath& output_file_path);
133 // Extracts the current entry to the given output directory path using
134 // ExtractCurrentEntryToFilePath(). Sub directories are created as needed
135 // based on the file path of the current entry. For example, if the file
136 // path in zip is "foo/bar.txt", and the output directory is "output",
137 // "output/foo/bar.txt" will be created.
139 // Returns true on success. OpenCurrentEntryInZip() must be called
141 bool ExtractCurrentEntryIntoDirectory(
142 const base::FilePath& output_directory_path);
144 #if defined(OS_POSIX)
145 // Extracts the current entry by writing directly to a file descriptor.
146 // Does not close the file descriptor. Returns true on success.
147 bool ExtractCurrentEntryToFd(int fd);
150 // Returns the current entry info. Returns NULL if the current entry is
151 // not yet opened. OpenCurrentEntryInZip() must be called beforehand.
152 EntryInfo* current_entry_info() const {
153 return current_entry_info_.get();
156 // Returns the number of entries in the zip file.
157 // Open() must be called beforehand.
158 int num_entries() const { return num_entries_; }
161 // Common code used both in Open and OpenFromFd.
164 // Resets the internal state.
170 scoped_ptr<EntryInfo> current_entry_info_;
172 DISALLOW_COPY_AND_ASSIGN(ZipReader);
177 #endif // THIRD_PARTY_ZLIB_GOOGLE_ZIP_READER_H_