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 | 4 |
5 #ifndef CHROME_COMMON_ZIP_READER_H_ | 5 #ifndef CHROME_COMMON_ZIP_READER_H_ |
6 #define CHROME_COMMON_ZIP_READER_H_ | 6 #define CHROME_COMMON_ZIP_READER_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include <string> | 9 #include <string> |
10 | 10 |
11 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
12 #include "base/file_path.h" | 12 #include "base/file_path.h" |
13 #include "base/file_util.h" | |
13 #include "base/memory/scoped_ptr.h" | 14 #include "base/memory/scoped_ptr.h" |
14 #include "base/time.h" | 15 #include "base/time.h" |
15 #include "third_party/zlib/contrib/minizip/unzip.h" | 16 #include "third_party/zlib/contrib/minizip/unzip.h" |
16 | 17 |
17 namespace zip { | 18 namespace zip { |
18 | 19 |
19 // This class is used for reading zip files. A typical use case of this | 20 // This class is used for reading zip files. A typical use case of this |
20 // class is to scan entries in a zip file and extract them. The code will | 21 // class is to scan entries in a zip file and extract them. The code will |
21 // look like: | 22 // look like: |
22 // | 23 // |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
70 DISALLOW_COPY_AND_ASSIGN(EntryInfo); | 71 DISALLOW_COPY_AND_ASSIGN(EntryInfo); |
71 }; | 72 }; |
72 | 73 |
73 ZipReader(); | 74 ZipReader(); |
74 ~ZipReader(); | 75 ~ZipReader(); |
75 | 76 |
76 // Opens the zip file specified by |zip_file_path|. Returns true on | 77 // Opens the zip file specified by |zip_file_path|. Returns true on |
77 // success. | 78 // success. |
78 bool Open(const FilePath& zip_file_path); | 79 bool Open(const FilePath& zip_file_path); |
79 | 80 |
81 #if defined(OS_POSIX) | |
82 // Opens the zip file referred to by the file descriptor |zip_fd|. | |
83 // Returns true on success. | |
84 bool OpenFd(const int zip_fd); | |
satorux1
2011/12/12 04:54:14
We often use "FromSomething" for naming functions
Jorge Lucangeli Obes
2011/12/12 23:34:35
Done.
| |
85 #endif | |
86 | |
80 // Closes the currently opened zip file. This function is called in the | 87 // Closes the currently opened zip file. This function is called in the |
81 // destructor of the class, so you usually don't need to call this. | 88 // destructor of the class, so you usually don't need to call this. |
82 void Close(); | 89 void Close(); |
83 | 90 |
84 // Returns true if there is at least one entry to read. This function is | 91 // Returns true if there is at least one entry to read. This function is |
85 // used to scan entries with AdvanceToNextEntry(), like: | 92 // used to scan entries with AdvanceToNextEntry(), like: |
86 // | 93 // |
87 // while (reader.HasMore()) { | 94 // while (reader.HasMore()) { |
88 // // Do something with the current file here. | 95 // // Do something with the current file here. |
89 // reader.AdvanceToNextEntry(); | 96 // reader.AdvanceToNextEntry(); |
(...skipping 28 matching lines...) Expand all Loading... | |
118 // Extracts the current entry to the given output directory path using | 125 // Extracts the current entry to the given output directory path using |
119 // ExtractCurrentEntryToFilePath(). Sub directories are created as needed | 126 // ExtractCurrentEntryToFilePath(). Sub directories are created as needed |
120 // based on the file path of the current entry. For example, if the file | 127 // based on the file path of the current entry. For example, if the file |
121 // path in zip is "foo/bar.txt", and the output directory is "output", | 128 // path in zip is "foo/bar.txt", and the output directory is "output", |
122 // "output/foo/bar.txt" will be created. | 129 // "output/foo/bar.txt" will be created. |
123 // | 130 // |
124 // Returns true on success. OpenCurrentEntryInZip() must be called | 131 // Returns true on success. OpenCurrentEntryInZip() must be called |
125 // beforehand. | 132 // beforehand. |
126 bool ExtractCurrentEntryIntoDirectory(const FilePath& output_directory_path); | 133 bool ExtractCurrentEntryIntoDirectory(const FilePath& output_directory_path); |
127 | 134 |
135 #if defined(OS_POSIX) | |
136 // Extracts the current entry by writing directly to a file descriptor. | |
137 // Does not close the file descriptor. Returns true on success. | |
138 bool ExtractCurrentEntryToFd(const int fd); | |
139 #endif | |
140 | |
128 // Returns the current entry info. Returns NULL if the current entry is | 141 // Returns the current entry info. Returns NULL if the current entry is |
129 // not yet opened. OpenCurrentEntryInZip() must be called beforehand. | 142 // not yet opened. OpenCurrentEntryInZip() must be called beforehand. |
130 EntryInfo* current_entry_info() const { | 143 EntryInfo* current_entry_info() const { |
131 return current_entry_info_.get(); | 144 return current_entry_info_.get(); |
132 } | 145 } |
133 | 146 |
134 // Returns the number of entries in the zip file. | 147 // Returns the number of entries in the zip file. |
135 // Open() must be called beforehand. | 148 // Open() must be called beforehand. |
136 int num_entries() const { return num_entries_; } | 149 int num_entries() const { return num_entries_; } |
137 | 150 |
138 private: | 151 private: |
152 // Initializes internal state. | |
153 bool Initialize(); | |
154 | |
139 // Resets the internal state. | 155 // Resets the internal state. |
140 void Reset(); | 156 void Reset(); |
141 | 157 |
158 // Helper for ExtractCurrentEntryTo(FilePath|Fd) | |
159 bool ExtractCurrentEntryToBuffer(const char* buf, const int size); | |
160 | |
142 unzFile zip_file_; | 161 unzFile zip_file_; |
143 int num_entries_; | 162 int num_entries_; |
144 bool reached_end_; | 163 bool reached_end_; |
145 scoped_ptr<EntryInfo> current_entry_info_; | 164 scoped_ptr<EntryInfo> current_entry_info_; |
146 | 165 |
147 DISALLOW_COPY_AND_ASSIGN(ZipReader); | 166 DISALLOW_COPY_AND_ASSIGN(ZipReader); |
148 }; | 167 }; |
149 | 168 |
150 } // namespace zip | 169 } // namespace zip |
151 | 170 |
152 #endif // CHROME_COMMON_ZIP_READER_H_ | 171 #endif // CHROME_COMMON_ZIP_READER_H_ |
OLD | NEW |