Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 #include "third_party/zlib/google/zip.h" | 5 #include "third_party/zlib/google/zip.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| 11 #include "base/files/file.h" | 11 #include "base/files/file.h" |
| 12 #include "base/files/file_enumerator.h" | 12 #include "base/files/file_enumerator.h" |
| 13 #include "base/logging.h" | 13 #include "base/logging.h" |
| 14 #include "base/strings/string16.h" | 14 #include "base/strings/string16.h" |
| 15 #include "base/strings/string_util.h" | 15 #include "base/strings/string_util.h" |
| 16 #include "third_party/zlib/google/zip_internal.h" | 16 #include "third_party/zlib/google/zip_internal.h" |
| 17 #include "third_party/zlib/google/zip_reader.h" | 17 #include "third_party/zlib/google/zip_reader.h" |
| 18 | 18 |
| 19 #if defined(USE_SYSTEM_MINIZIP) | 19 #if defined(USE_SYSTEM_MINIZIP) |
| 20 #include <minizip/unzip.h> | 20 #include <minizip/unzip.h> |
| 21 #include <minizip/zip.h> | 21 #include <minizip/zip.h> |
| 22 #else | 22 #else |
| 23 #include "third_party/zlib/contrib/minizip/unzip.h" | 23 #include "third_party/zlib/contrib/minizip/unzip.h" |
| 24 #include "third_party/zlib/contrib/minizip/zip.h" | 24 #include "third_party/zlib/contrib/minizip/zip.h" |
| 25 #endif | 25 #endif |
| 26 | 26 |
| 27 namespace { | 27 namespace { |
| 28 | 28 |
| 29 // Returns a zip_fileinfo struct with the time represented by |file_time|. | |
| 30 zip_fileinfo TimeToZipFileInfo(const base::Time& file_time) { | |
| 31 base::Time::Exploded file_time_parts; | |
| 32 file_time.LocalExplode(&file_time_parts); | |
| 33 | |
| 34 zip_fileinfo zip_info = {}; | |
| 35 if (file_time_parts.year >= 1980) { | |
| 36 // This if check works around the handling of the year value in | |
| 37 // contrib/minizip/zip.c in function zip64local_TmzDateToDosDate | |
| 38 // It assumes that dates below 1980 are in the double digit format. | |
| 39 // Hence the fail safe option is to leave the date unset. Some programs | |
| 40 // might show the unset date as 1980-0-0 which is invalid. | |
| 41 zip_info.tmz_date.tm_year = file_time_parts.year; | |
| 42 zip_info.tmz_date.tm_mon = file_time_parts.month - 1; | |
| 43 zip_info.tmz_date.tm_mday = file_time_parts.day_of_month; | |
| 44 zip_info.tmz_date.tm_hour = file_time_parts.hour; | |
| 45 zip_info.tmz_date.tm_min = file_time_parts.minute; | |
| 46 zip_info.tmz_date.tm_sec = file_time_parts.second; | |
| 47 } | |
| 48 | |
| 49 return zip_info; | |
| 50 } | |
| 51 | |
| 52 // Returns a zip_fileinfo with the last modification date of |path| set. | |
| 53 zip_fileinfo GetFileInfoForZipping(const base::FilePath& path) { | |
| 54 base::Time file_time; | |
| 55 base::File::Info file_info; | |
| 56 if (base::GetFileInfo(path, &file_info)) | |
| 57 file_time = file_info.last_modified; | |
| 58 return TimeToZipFileInfo(file_time); | |
| 59 } | |
| 60 | |
| 61 bool AddFileToZip(zipFile zip_file, const base::FilePath& src_dir) { | 29 bool AddFileToZip(zipFile zip_file, const base::FilePath& src_dir) { |
| 62 base::File file(src_dir, base::File::FLAG_OPEN | base::File::FLAG_READ); | 30 base::File file(src_dir, base::File::FLAG_OPEN | base::File::FLAG_READ); |
| 63 if (!file.IsValid()) { | 31 if (!file.IsValid()) { |
| 64 DLOG(ERROR) << "Could not open file for path " << src_dir.value(); | 32 DLOG(ERROR) << "Could not open file for path " << src_dir.value(); |
| 65 return false; | 33 return false; |
| 66 } | 34 } |
| 67 | 35 |
| 68 int num_bytes; | 36 int num_bytes; |
| 69 char buf[zip::internal::kZipBufSize]; | 37 char buf[zip::internal::kZipBufSize]; |
| 70 do { | 38 do { |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 88 DCHECK(result); | 56 DCHECK(result); |
| 89 std::string str_path = relative_path.AsUTF8Unsafe(); | 57 std::string str_path = relative_path.AsUTF8Unsafe(); |
| 90 #if defined(OS_WIN) | 58 #if defined(OS_WIN) |
| 91 ReplaceSubstringsAfterOffset(&str_path, 0u, "\\", "/"); | 59 ReplaceSubstringsAfterOffset(&str_path, 0u, "\\", "/"); |
| 92 #endif | 60 #endif |
| 93 | 61 |
| 94 bool is_directory = base::DirectoryExists(path); | 62 bool is_directory = base::DirectoryExists(path); |
| 95 if (is_directory) | 63 if (is_directory) |
| 96 str_path += "/"; | 64 str_path += "/"; |
| 97 | 65 |
| 98 // Section 4.4.4 http://www.pkware.com/documents/casestudies/APPNOTE.TXT | 66 zip_fileinfo file_info = zip::internal::GetFileInfoForZipping(path); |
| 99 // Setting the Language encoding flag so the file is told to be in utf-8. | 67 if (!zip::internal::ZipOpenNewFileInZipWrapper( |
| 100 const uLong LANGUAGE_ENCODING_FLAG = 0x1 << 11; | 68 zip_file, str_path, &file_info)) |
| 101 | |
| 102 zip_fileinfo file_info = GetFileInfoForZipping(path); | |
| 103 | |
| 104 if (ZIP_OK != zipOpenNewFileInZip4( | |
| 105 zip_file, // file | |
| 106 str_path.c_str(), // filename | |
| 107 &file_info, // zipfi | |
| 108 NULL, // extrafield_local, | |
| 109 0u, // size_extrafield_local | |
| 110 NULL, // extrafield_global | |
| 111 0u, // size_extrafield_global | |
| 112 NULL, // comment | |
| 113 Z_DEFLATED, // method | |
| 114 Z_DEFAULT_COMPRESSION, // level | |
| 115 0, // raw | |
| 116 -MAX_WBITS, // windowBits | |
| 117 DEF_MEM_LEVEL, // memLevel | |
| 118 Z_DEFAULT_STRATEGY, // strategy | |
| 119 NULL, // password | |
| 120 0, // crcForCrypting | |
| 121 0, // versionMadeBy | |
| 122 LANGUAGE_ENCODING_FLAG)) { // flagBase | |
| 123 DLOG(ERROR) << "Could not open zip file entry " << str_path; | |
| 124 return false; | 69 return false; |
| 125 } | |
| 126 | 70 |
| 127 bool success = true; | 71 bool success = true; |
| 128 if (!is_directory) { | 72 if (!is_directory) { |
| 129 success = AddFileToZip(zip_file, path); | 73 success = AddFileToZip(zip_file, path); |
| 130 } | 74 } |
| 131 | 75 |
| 132 if (ZIP_OK != zipCloseFileInZip(zip_file)) { | 76 if (ZIP_OK != zipCloseFileInZip(zip_file)) { |
| 133 DLOG(ERROR) << "Could not close zip file entry " << str_path; | 77 DLOG(ERROR) << "Could not close zip file entry " << str_path; |
| 134 return false; | 78 return false; |
| 135 } | 79 } |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 187 APPEND_STATUS_CREATE); | 131 APPEND_STATUS_CREATE); |
| 188 | 132 |
| 189 if (!zip_file) { | 133 if (!zip_file) { |
| 190 DLOG(WARNING) << "couldn't create file " << dest_file.value(); | 134 DLOG(WARNING) << "couldn't create file " << dest_file.value(); |
| 191 return false; | 135 return false; |
| 192 } | 136 } |
| 193 | 137 |
| 194 bool success = true; | 138 bool success = true; |
| 195 base::FileEnumerator file_enumerator(src_dir, true /* recursive */, | 139 base::FileEnumerator file_enumerator(src_dir, true /* recursive */, |
| 196 base::FileEnumerator::FILES | base::FileEnumerator::DIRECTORIES); | 140 base::FileEnumerator::FILES | base::FileEnumerator::DIRECTORIES); |
| 197 for (base::FilePath path = file_enumerator.Next(); !path.value().empty(); | 141 for (base::FilePath path = file_enumerator.Next(); |
| 142 !path.value().empty() && success; | |
|
satorux1
2014/04/03 05:10:54
one more space?
| |
| 198 path = file_enumerator.Next()) { | 143 path = file_enumerator.Next()) { |
| 199 if (!filter_cb.Run(path)) { | 144 if (!filter_cb.Run(path)) { |
| 200 continue; | 145 continue; |
| 201 } | 146 } |
| 202 | 147 |
| 203 if (!AddEntryToZip(zip_file, path, src_dir)) { | 148 if (!AddEntryToZip(zip_file, path, src_dir)) { |
| 204 success = false; | 149 success = false; |
|
satorux1
2014/04/03 05:10:54
add break here? then you don't need to check 'succ
| |
| 205 return false; | |
| 206 } | 150 } |
| 207 } | 151 } |
| 208 | 152 |
| 209 if (ZIP_OK != zipClose(zip_file, NULL)) { | 153 if (ZIP_OK != zipClose(zip_file, NULL)) { |
| 210 DLOG(ERROR) << "Error closing zip file " << dest_file.value(); | 154 DLOG(ERROR) << "Error closing zip file " << dest_file.value(); |
| 211 return false; | 155 return false; |
| 212 } | 156 } |
| 213 | 157 |
| 214 return success; | 158 return success; |
| 215 } | 159 } |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 252 if (ZIP_OK != zipClose(zip_file, NULL)) { | 196 if (ZIP_OK != zipClose(zip_file, NULL)) { |
| 253 DLOG(ERROR) << "Error closing zip file for fd " << dest_fd; | 197 DLOG(ERROR) << "Error closing zip file for fd " << dest_fd; |
| 254 success = false; | 198 success = false; |
| 255 } | 199 } |
| 256 | 200 |
| 257 return success; | 201 return success; |
| 258 } | 202 } |
| 259 #endif // defined(OS_POSIX) | 203 #endif // defined(OS_POSIX) |
| 260 | 204 |
| 261 } // namespace zip | 205 } // namespace zip |
| OLD | NEW |