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 // 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::PlatformFileInfo file_info; |
| 57 if (base::GetFileInfo(path, &file_info)) |
| 58 file_time = file_info.last_modified; |
| 59 return TimeToZipFileInfo(file_time); |
| 60 } |
| 61 |
27 bool AddFileToZip(zipFile zip_file, const base::FilePath& src_dir) { | 62 bool AddFileToZip(zipFile zip_file, const base::FilePath& src_dir) { |
28 net::FileStream stream(NULL); | 63 net::FileStream stream(NULL); |
29 int flags = base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_READ; | 64 int flags = base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_READ; |
30 if (stream.OpenSync(src_dir, flags) != 0) { | 65 if (stream.OpenSync(src_dir, flags) != 0) { |
31 DLOG(ERROR) << "Could not open stream for path " | 66 DLOG(ERROR) << "Could not open stream for path " |
32 << src_dir.value(); | 67 << src_dir.value(); |
33 return false; | 68 return false; |
34 } | 69 } |
35 | 70 |
36 int num_bytes; | 71 int num_bytes; |
(...skipping 21 matching lines...) Expand all Loading... |
58 #if defined(OS_WIN) | 93 #if defined(OS_WIN) |
59 ReplaceSubstringsAfterOffset(&str_path, 0u, "\\", "/"); | 94 ReplaceSubstringsAfterOffset(&str_path, 0u, "\\", "/"); |
60 #endif | 95 #endif |
61 | 96 |
62 bool is_directory = base::DirectoryExists(path); | 97 bool is_directory = base::DirectoryExists(path); |
63 if (is_directory) | 98 if (is_directory) |
64 str_path += "/"; | 99 str_path += "/"; |
65 | 100 |
66 // Section 4.4.4 http://www.pkware.com/documents/casestudies/APPNOTE.TXT | 101 // 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. | 102 // Setting the Language encoding flag so the file is told to be in utf-8. |
68 const unsigned long LANGUAGE_ENCODING_FLAG = 0x1 << 11; | 103 const uLong LANGUAGE_ENCODING_FLAG = 0x1 << 11; |
| 104 |
| 105 zip_fileinfo file_info = GetFileInfoForZipping(path); |
69 | 106 |
70 if (ZIP_OK != zipOpenNewFileInZip4( | 107 if (ZIP_OK != zipOpenNewFileInZip4( |
71 zip_file, //file | 108 zip_file, // file |
72 str_path.c_str(), // filename | 109 str_path.c_str(), // filename |
73 NULL, // zipfi (file_info) | 110 &file_info, // zipfi |
74 NULL, // extrafield_local, | 111 NULL, // extrafield_local, |
75 0u, // size_extrafield_local | 112 0u, // size_extrafield_local |
76 NULL, // extrafield_global | 113 NULL, // extrafield_global |
77 0u, // size_extrafield_global | 114 0u, // size_extrafield_global |
78 NULL, // comment | 115 NULL, // comment |
79 Z_DEFLATED, // method | 116 Z_DEFLATED, // method |
80 Z_DEFAULT_COMPRESSION, // level | 117 Z_DEFAULT_COMPRESSION, // level |
81 0, // raw | 118 0, // raw |
82 -MAX_WBITS, // windowBits | 119 -MAX_WBITS, // windowBits |
83 DEF_MEM_LEVEL, // memLevel | 120 DEF_MEM_LEVEL, // memLevel |
84 Z_DEFAULT_STRATEGY, // strategy | 121 Z_DEFAULT_STRATEGY, // strategy |
85 NULL, //password | 122 NULL, // password |
86 0, // crcForCrypting | 123 0, // crcForCrypting |
87 0, // versionMadeBy | 124 0, // versionMadeBy |
88 LANGUAGE_ENCODING_FLAG)) { // flagBase | 125 LANGUAGE_ENCODING_FLAG)) { // flagBase |
89 DLOG(ERROR) << "Could not open zip file entry " << str_path; | 126 DLOG(ERROR) << "Could not open zip file entry " << str_path; |
90 return false; | 127 return false; |
91 } | 128 } |
92 | 129 |
93 bool success = true; | 130 bool success = true; |
94 if (!is_directory) { | 131 if (!is_directory) { |
95 success = AddFileToZip(zip_file, path); | 132 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)) { | 255 if (ZIP_OK != zipClose(zip_file, NULL)) { |
219 DLOG(ERROR) << "Error closing zip file for fd " << dest_fd; | 256 DLOG(ERROR) << "Error closing zip file for fd " << dest_fd; |
220 success = false; | 257 success = false; |
221 } | 258 } |
222 | 259 |
223 return success; | 260 return success; |
224 } | 261 } |
225 #endif // defined(OS_POSIX) | 262 #endif // defined(OS_POSIX) |
226 | 263 |
227 } // namespace zip | 264 } // namespace zip |
OLD | NEW |