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