Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(5)

Side by Side Diff: chrome/common/zip_reader.h

Issue 8873039: Add an API to unpack Zip files directly from and to file descriptors. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Fixed lint errors. Created 9 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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 OpenFromFd(int zip_fd);
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
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(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 // Common code used both in Open and OpenFromFd.
153 bool OpenInternal();
154
139 // Resets the internal state. 155 // Resets the internal state.
140 void Reset(); 156 void Reset();
141 157
142 unzFile zip_file_; 158 unzFile zip_file_;
143 int num_entries_; 159 int num_entries_;
144 bool reached_end_; 160 bool reached_end_;
145 scoped_ptr<EntryInfo> current_entry_info_; 161 scoped_ptr<EntryInfo> current_entry_info_;
146 162
147 DISALLOW_COPY_AND_ASSIGN(ZipReader); 163 DISALLOW_COPY_AND_ASSIGN(ZipReader);
148 }; 164 };
149 165
150 } // namespace zip 166 } // namespace zip
151 167
152 #endif // CHROME_COMMON_ZIP_READER_H_ 168 #endif // CHROME_COMMON_ZIP_READER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698