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> | |
| 8 #include <vector> | |
| 9 | |
| 7 #include "base/bind.h" | 10 #include "base/bind.h" |
| 8 #include "base/file_util.h" | 11 #include "base/file_util.h" |
| 9 #include "base/files/file_enumerator.h" | 12 #include "base/files/file_enumerator.h" |
| 10 #include "base/logging.h" | 13 #include "base/logging.h" |
| 11 #include "base/strings/string16.h" | 14 #include "base/strings/string16.h" |
| 12 #include "base/strings/string_util.h" | 15 #include "base/strings/string_util.h" |
| 13 #include "net/base/file_stream.h" | 16 #include "net/base/file_stream.h" |
| 14 #include "third_party/zlib/google/zip_internal.h" | 17 #include "third_party/zlib/google/zip_internal.h" |
| 15 #include "third_party/zlib/google/zip_reader.h" | 18 #include "third_party/zlib/google/zip_reader.h" |
| 16 | 19 |
| 17 #if defined(USE_SYSTEM_MINIZIP) | 20 #if defined(USE_SYSTEM_MINIZIP) |
| 18 #include <minizip/unzip.h> | 21 #include <minizip/unzip.h> |
| 19 #include <minizip/zip.h> | 22 #include <minizip/zip.h> |
| 20 #else | 23 #else |
| 21 #include "third_party/zlib/contrib/minizip/unzip.h" | 24 #include "third_party/zlib/contrib/minizip/unzip.h" |
| 22 #include "third_party/zlib/contrib/minizip/zip.h" | 25 #include "third_party/zlib/contrib/minizip/zip.h" |
| 23 #endif | 26 #endif |
| 24 | 27 |
| 25 namespace { | 28 namespace { |
| 26 | 29 |
| 30 zip_fileinfo TimeToZipFileInfo(const base::Time& file_time) { | |
|
satorux1
2013/12/12 08:03:57
function comment is missing.
http://google-styleg
| |
| 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 just to allow the default date (epoch) | |
| 40 // to be set. | |
| 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 zip_fileinfo GetFileInfoForZipping(const base::FilePath& path) { | |
|
satorux1
2013/12/12 08:03:57
ditto
| |
| 53 base::Time file_time; | |
| 54 base::PlatformFileInfo file_info; | |
| 55 if (base::GetFileInfo(path, &file_info)) | |
| 56 file_time = file_info.last_modified; | |
| 57 return TimeToZipFileInfo(file_time); | |
| 58 } | |
| 59 | |
| 27 bool AddFileToZip(zipFile zip_file, const base::FilePath& src_dir) { | 60 bool AddFileToZip(zipFile zip_file, const base::FilePath& src_dir) { |
| 28 net::FileStream stream(NULL); | 61 net::FileStream stream(NULL); |
| 29 int flags = base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_READ; | 62 int flags = base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_READ; |
| 30 if (stream.OpenSync(src_dir, flags) != 0) { | 63 if (stream.OpenSync(src_dir, flags) != 0) { |
| 31 DLOG(ERROR) << "Could not open stream for path " | 64 DLOG(ERROR) << "Could not open stream for path " |
| 32 << src_dir.value(); | 65 << src_dir.value(); |
| 33 return false; | 66 return false; |
| 34 } | 67 } |
| 35 | 68 |
| 36 int num_bytes; | 69 int num_bytes; |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 58 #if defined(OS_WIN) | 91 #if defined(OS_WIN) |
| 59 ReplaceSubstringsAfterOffset(&str_path, 0u, "\\", "/"); | 92 ReplaceSubstringsAfterOffset(&str_path, 0u, "\\", "/"); |
| 60 #endif | 93 #endif |
| 61 | 94 |
| 62 bool is_directory = base::DirectoryExists(path); | 95 bool is_directory = base::DirectoryExists(path); |
| 63 if (is_directory) | 96 if (is_directory) |
| 64 str_path += "/"; | 97 str_path += "/"; |
| 65 | 98 |
| 66 // Section 4.4.4 http://www.pkware.com/documents/casestudies/APPNOTE.TXT | 99 // Section 4.4.4 http://www.pkware.com/documents/casestudies/APPNOTE.TXT |
| 67 // Setting the Language encoding flag so the file is told to be in utf-8. | 100 // Setting the Language encoding flag so the file is told to be in utf-8. |
| 68 const unsigned long LANGUAGE_ENCODING_FLAG = 0x1 << 11; | 101 const uLong LANGUAGE_ENCODING_FLAG = 0x1 << 11; |
| 102 | |
| 103 zip_fileinfo file_info = GetFileInfoForZipping(path); | |
| 69 | 104 |
| 70 if (ZIP_OK != zipOpenNewFileInZip4( | 105 if (ZIP_OK != zipOpenNewFileInZip4( |
| 71 zip_file, //file | 106 zip_file, // file |
| 72 str_path.c_str(), // filename | 107 str_path.c_str(), // filename |
| 73 NULL, // zipfi (file_info) | 108 &file_info, // zipfi |
| 74 NULL, // extrafield_local, | 109 NULL, // extrafield_local, |
| 75 0u, // size_extrafield_local | 110 0u, // size_extrafield_local |
| 76 NULL, // extrafield_global | 111 NULL, // extrafield_global |
| 77 0u, // size_extrafield_global | 112 0u, // size_extrafield_global |
| 78 NULL, // comment | 113 NULL, // comment |
| 79 Z_DEFLATED, // method | 114 Z_DEFLATED, // method |
| 80 Z_DEFAULT_COMPRESSION, // level | 115 Z_DEFAULT_COMPRESSION, // level |
| 81 0, // raw | 116 0, // raw |
| 82 -MAX_WBITS, // windowBits | 117 -MAX_WBITS, // windowBits |
| 83 DEF_MEM_LEVEL, // memLevel | 118 DEF_MEM_LEVEL, // memLevel |
| 84 Z_DEFAULT_STRATEGY, // strategy | 119 Z_DEFAULT_STRATEGY, // strategy |
| 85 NULL, //password | 120 NULL, // password |
| 86 0, // crcForCrypting | 121 0, // crcForCrypting |
| 87 0, // versionMadeBy | 122 0, // versionMadeBy |
| 88 LANGUAGE_ENCODING_FLAG)) { // flagBase | 123 LANGUAGE_ENCODING_FLAG)) { // flagBase |
| 89 DLOG(ERROR) << "Could not open zip file entry " << str_path; | 124 DLOG(ERROR) << "Could not open zip file entry " << str_path; |
| 90 return false; | 125 return false; |
| 91 } | 126 } |
| 92 | 127 |
| 93 bool success = true; | 128 bool success = true; |
| 94 if (!is_directory) { | 129 if (!is_directory) { |
| 95 success = AddFileToZip(zip_file, path); | 130 success = AddFileToZip(zip_file, path); |
| (...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 218 if (ZIP_OK != zipClose(zip_file, NULL)) { | 253 if (ZIP_OK != zipClose(zip_file, NULL)) { |
| 219 DLOG(ERROR) << "Error closing zip file for fd " << dest_fd; | 254 DLOG(ERROR) << "Error closing zip file for fd " << dest_fd; |
| 220 success = false; | 255 success = false; |
| 221 } | 256 } |
| 222 | 257 |
| 223 return success; | 258 return success; |
| 224 } | 259 } |
| 225 #endif // defined(OS_POSIX) | 260 #endif // defined(OS_POSIX) |
| 226 | 261 |
| 227 } // namespace zip | 262 } // namespace zip |
| OLD | NEW |