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

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

Issue 8508003: zip: Add ZipReader and rework Unzip() using the new class. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 9 years, 1 month 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 | Annotate | Revision Log
OLDNEW
(Empty)
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
5 #include "chrome/common/zip_reader.h"
6
7 #include "base/file_util.h"
8 #include "base/logging.h"
9 #include "base/string_util.h"
10 #include "base/utf_string_conversions.h"
11 #include "chrome/common/zip_internal.h"
12 #include "net/base/file_stream.h"
13 #include "third_party/zlib/contrib/minizip/unzip.h"
14 #if defined(OS_WIN)
15 #include "third_party/zlib/contrib/minizip/iowin32.h"
16 #endif
17
18 namespace zip {
19
20 // TODO(satorux): The implementation assumes that file names in zip files
21 // are encoded in UTF-8. This is true for zip files created by Zip()
22 // function in zip.h, but not true for user-supplied random zip files.
23 ZipReader::FileInfo::FileInfo(const std::string& file_name_in_zip,
24 const unz_file_info& raw_file_info)
25 : file_path_(FilePath::FromUTF8Unsafe(file_name_in_zip)),
26 is_directory_(false) {
27 original_size_ = raw_file_info.uncompressed_size;
28
29 // Directory entries in zip files end with "/".
30 is_directory_ = EndsWith(file_name_in_zip, "/", false);
31 // Check the file name here for directory traversal issues. In the name of
Aaron Boodman 2011/11/09 08:00:40 Nit: add blank line before this one.
satorux1 2011/11/10 08:00:56 Done.
satorux1 2011/11/10 08:00:56 Done.
32 // simplicity and security, we might reject a valid file name such as "a..b".
33 is_unsafe_ = file_name_in_zip.find("..") != std::string::npos;
34
35 // We also consider that the file name is unsafe, if it's invalid UTF-8.
36 string16 file_name_utf16;
37 if (!UTF8ToUTF16(file_name_in_zip.data(), file_name_in_zip.size(),
38 &file_name_utf16))
Aaron Boodman 2011/11/09 08:00:40 If a conditional wraps, it should use curly braces
satorux1 2011/11/10 08:00:56 Done.
satorux1 2011/11/10 08:00:56 Done.
39 is_unsafe_ = true;
40
41 // Construct the last modified time. The timezone info is not present in
42 // zip files, so we construct the time as local time.
43 base::Time::Exploded exploded_time = {}; // Zero-clear.
44 exploded_time.year = raw_file_info.tmu_date.tm_year;
45 // The month in zip file is 0-based, whereas ours is 1-based.
46 exploded_time.month = raw_file_info.tmu_date.tm_mon + 1;
47 exploded_time.day_of_month = raw_file_info.tmu_date.tm_mday;
48 exploded_time.hour = raw_file_info.tmu_date.tm_hour;
49 exploded_time.minute = raw_file_info.tmu_date.tm_min;
50 exploded_time.second = raw_file_info.tmu_date.tm_sec;
51 exploded_time.millisecond = 0;
52 last_modified_ = base::Time::FromLocalExploded(exploded_time);
53 }
54
55 // Don't forget to update Close() when you add new code in the constructor.
Aaron Boodman 2011/11/09 08:00:40 Instead of comments like this, it is better to hav
satorux1 2011/11/10 08:00:56 You are right. Done.
56 ZipReader::ZipReader()
57 : zip_file_(NULL),
58 num_files_(0),
59 reached_end_(false),
60 current_file_info_(NULL) {
61 }
62
63 ZipReader::~ZipReader() {
64 Close();
65 }
66
67 bool ZipReader::Open(const FilePath& zip_file_path) {
68 DCHECK(!zip_file_);
69
70 // Use of "Unsafe" function does not look good, but there is no way to do
71 // this safely on Linux. See file_util.h for details.
72 zip_file_ = internal::OpenForUnzipping(zip_file_path.AsUTF8Unsafe());
73 if (!zip_file_) {
74 return false;
75 }
76
77 unz_global_info zip_info = {}; // Zero-clear.
Aaron Boodman 2011/11/09 08:00:40 Unnecessary comment.
satorux1 2011/11/10 08:00:56 Done.
78 if (unzGetGlobalInfo(zip_file_, &zip_info) != UNZ_OK) {
79 return false;
80 }
81 num_files_ = zip_info.number_entry;
82 if (num_files_ < 0)
83 return false;
84
85 // We are already at the end if the zip file is empty.
86 reached_end_ = (num_files_ == 0);
87 return true;
88 }
89
90 void ZipReader::Close() {
91 if (zip_file_) {
92 unzClose(zip_file_);
93 zip_file_ = NULL;
94 }
95 num_files_ = 0;
96 reached_end_ = false;
97 current_file_info_.reset();
98 }
99
100 bool ZipReader::HasMore() {
101 return !reached_end_;
102 }
103
104 bool ZipReader::AdvanceToNextFile() {
105 DCHECK(zip_file_);
106
107 // Should not go further if we already reached the end.
108 if (reached_end_)
109 return false;
110
111 unz_file_pos position = {};
112 if (unzGetFilePos(zip_file_, &position) != UNZ_OK)
113 return false;
114 const int current_file_index = position.num_of_file;
115 // If we are currently at the last of the file, then the next position
116 // is the end of the zip file, so mark that we reached the end.
117 if (current_file_index + 1 == num_files_) {
118 reached_end_ = true;
119 } else {
120 DCHECK_LT(current_file_index + 1, num_files_);
121 if (unzGoToNextFile(zip_file_) != UNZ_OK) {
122 return false;
123 }
124 }
125 current_file_info_.reset();
126 return true;
127 }
128
129 bool ZipReader::OpenCurrentFileInZip() {
130 DCHECK(zip_file_);
131
132 unz_file_info raw_file_info = {}; // Zero-clear.
133 char raw_file_name_in_zip[internal::kZipMaxPath] = {};
134 const int result = unzGetCurrentFileInfo(zip_file_,
135 &raw_file_info,
136 raw_file_name_in_zip,
137 sizeof(raw_file_name_in_zip) - 1,
138 NULL, // extraField.
139 0, // extraFieldBufferSize.
140 NULL, // szComment.
141 0); // commentBufferSize.
142 if (result != UNZ_OK)
143 return NULL;
144 if (raw_file_name_in_zip[0] == '\0')
145 return NULL;
146 current_file_info_.reset(new FileInfo(raw_file_name_in_zip, raw_file_info));
147 return true;
148 }
149
150 bool ZipReader::LocateAndOpenFile(const FilePath& path_in_zip) {
151 DCHECK(zip_file_);
152
153 current_file_info_.reset();
154 reached_end_ = false;
155 const int kDefaultCaseSensivityOfOS = 0;
156 const int result = unzLocateFile(zip_file_,
157 path_in_zip.AsUTF8Unsafe().c_str(),
158 kDefaultCaseSensivityOfOS);
159 if (result != UNZ_OK)
160 return false;
161
162 // Then Open the file.
163 return OpenCurrentFileInZip();
164 }
165
166 bool ZipReader::ExtractCurrentFileToFile(
167 const FilePath& output_file_path) {
168 DCHECK(zip_file_);
169
170 // If this is a directory, just create it and return.
171 if (current_file_info()->is_directory())
172 return file_util::CreateDirectory(output_file_path);
173
174 const int open_result = unzOpenCurrentFile(zip_file_);
175 if (open_result != UNZ_OK)
176 return false;
177
178 // We can't rely on parent directory entries being specified in the
179 // zip, so we make sure they are created.
180 FilePath output_dir_path = output_file_path.DirName();
181 if (!file_util::CreateDirectory(output_dir_path))
182 return false;
183
184 net::FileStream stream;
185 const int flags = (base::PLATFORM_FILE_CREATE_ALWAYS |
186 base::PLATFORM_FILE_WRITE);
187 if (stream.Open(output_file_path, flags) != 0)
188 return false;
189
190 bool success = true; // This becomes false when something bad happens.
191 while (true) {
192 char buf[internal::kZipBufSize];
193 const int num_bytes_read = unzReadCurrentFile(zip_file_, buf,
194 internal::kZipBufSize);
195 if (num_bytes_read == 0) {
196 // Reached the end of the file.
197 break;
198 } else if (num_bytes_read < 0) {
199 // If num_bytes_read < 0, then it's a specific UNZ_* error code.
200 success = false;
201 break;
202 } else if (num_bytes_read > 0) {
203 // Some data is read. Write it to the output file.
204 if (num_bytes_read != stream.Write(buf, num_bytes_read,
205 net::CompletionCallback())) {
206 success = false;
207 break;
208 }
209 }
210 }
211
212 stream.Close();
213 unzCloseCurrentFile(zip_file_);
214 return success;
215 }
216
217 bool ZipReader::ExtractCurrentFileToDirectory(
218 const FilePath& output_directory_path) {
219 DCHECK(current_file_info_.get());
220
221 FilePath output_file_path = output_directory_path.Append(
222 current_file_info()->file_path());
223 return ExtractCurrentFileToFile(output_file_path);
224 }
225
226 } // namespace zip
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698