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 /** | |
31 * Returns a zip_fileinfo struct with the time represented by |file_time|. | |
32 */ | |
satorux1
2013/12/13 04:52:50
Please use // for comments
João Eiras
2013/12/17 10:15:38
Done.
| |
33 zip_fileinfo TimeToZipFileInfo(const base::Time& file_time) { | |
34 base::Time::Exploded file_time_parts; | |
35 file_time.LocalExplode(&file_time_parts); | |
36 | |
37 zip_fileinfo zip_info = {}; | |
38 if (file_time_parts.year >= 1980) { | |
39 // This if check works around the handling of the year value in | |
40 // contrib/minizip/zip.c in function zip64local_TmzDateToDosDate | |
41 // It assumes that dates below 1980 are in the double digit format. | |
42 // Hence the fail safe option is to leave the date unset. Some programs | |
43 // might show the unset date as 1980-0-0 which is invalid. | |
44 zip_info.tmz_date.tm_year = file_time_parts.year; | |
45 zip_info.tmz_date.tm_mon = file_time_parts.month - 1; | |
46 zip_info.tmz_date.tm_mday = file_time_parts.day_of_month; | |
47 zip_info.tmz_date.tm_hour = file_time_parts.hour; | |
48 zip_info.tmz_date.tm_min = file_time_parts.minute; | |
49 zip_info.tmz_date.tm_sec = file_time_parts.second; | |
50 } | |
51 | |
52 return zip_info; | |
53 } | |
54 | |
55 /** | |
56 * Returns a zip_fileinfo with the last modification date of |path| set. | |
57 */ | |
satorux1
2013/12/13 04:52:50
ditto
João Eiras
2013/12/17 10:15:38
Done.
| |
58 zip_fileinfo GetFileInfoForZipping(const base::FilePath& path) { | |
59 base::Time file_time; | |
60 base::PlatformFileInfo file_info; | |
61 if (base::GetFileInfo(path, &file_info)) | |
62 file_time = file_info.last_modified; | |
63 return TimeToZipFileInfo(file_time); | |
64 } | |
65 | |
27 bool AddFileToZip(zipFile zip_file, const base::FilePath& src_dir) { | 66 bool AddFileToZip(zipFile zip_file, const base::FilePath& src_dir) { |
28 net::FileStream stream(NULL); | 67 net::FileStream stream(NULL); |
29 int flags = base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_READ; | 68 int flags = base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_READ; |
30 if (stream.OpenSync(src_dir, flags) != 0) { | 69 if (stream.OpenSync(src_dir, flags) != 0) { |
31 DLOG(ERROR) << "Could not open stream for path " | 70 DLOG(ERROR) << "Could not open stream for path " |
32 << src_dir.value(); | 71 << src_dir.value(); |
33 return false; | 72 return false; |
34 } | 73 } |
35 | 74 |
36 int num_bytes; | 75 int num_bytes; |
(...skipping 21 matching lines...) Expand all Loading... | |
58 #if defined(OS_WIN) | 97 #if defined(OS_WIN) |
59 ReplaceSubstringsAfterOffset(&str_path, 0u, "\\", "/"); | 98 ReplaceSubstringsAfterOffset(&str_path, 0u, "\\", "/"); |
60 #endif | 99 #endif |
61 | 100 |
62 bool is_directory = base::DirectoryExists(path); | 101 bool is_directory = base::DirectoryExists(path); |
63 if (is_directory) | 102 if (is_directory) |
64 str_path += "/"; | 103 str_path += "/"; |
65 | 104 |
66 // Section 4.4.4 http://www.pkware.com/documents/casestudies/APPNOTE.TXT | 105 // 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. | 106 // Setting the Language encoding flag so the file is told to be in utf-8. |
68 const unsigned long LANGUAGE_ENCODING_FLAG = 0x1 << 11; | 107 const uLong LANGUAGE_ENCODING_FLAG = 0x1 << 11; |
108 | |
109 zip_fileinfo file_info = GetFileInfoForZipping(path); | |
69 | 110 |
70 if (ZIP_OK != zipOpenNewFileInZip4( | 111 if (ZIP_OK != zipOpenNewFileInZip4( |
71 zip_file, //file | 112 zip_file, // file |
72 str_path.c_str(), // filename | 113 str_path.c_str(), // filename |
73 NULL, // zipfi (file_info) | 114 &file_info, // zipfi |
74 NULL, // extrafield_local, | 115 NULL, // extrafield_local, |
75 0u, // size_extrafield_local | 116 0u, // size_extrafield_local |
76 NULL, // extrafield_global | 117 NULL, // extrafield_global |
77 0u, // size_extrafield_global | 118 0u, // size_extrafield_global |
78 NULL, // comment | 119 NULL, // comment |
79 Z_DEFLATED, // method | 120 Z_DEFLATED, // method |
80 Z_DEFAULT_COMPRESSION, // level | 121 Z_DEFAULT_COMPRESSION, // level |
81 0, // raw | 122 0, // raw |
82 -MAX_WBITS, // windowBits | 123 -MAX_WBITS, // windowBits |
83 DEF_MEM_LEVEL, // memLevel | 124 DEF_MEM_LEVEL, // memLevel |
84 Z_DEFAULT_STRATEGY, // strategy | 125 Z_DEFAULT_STRATEGY, // strategy |
85 NULL, //password | 126 NULL, // password |
86 0, // crcForCrypting | 127 0, // crcForCrypting |
87 0, // versionMadeBy | 128 0, // versionMadeBy |
88 LANGUAGE_ENCODING_FLAG)) { // flagBase | 129 LANGUAGE_ENCODING_FLAG)) { // flagBase |
89 DLOG(ERROR) << "Could not open zip file entry " << str_path; | 130 DLOG(ERROR) << "Could not open zip file entry " << str_path; |
90 return false; | 131 return false; |
91 } | 132 } |
92 | 133 |
93 bool success = true; | 134 bool success = true; |
94 if (!is_directory) { | 135 if (!is_directory) { |
95 success = AddFileToZip(zip_file, path); | 136 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)) { | 259 if (ZIP_OK != zipClose(zip_file, NULL)) { |
219 DLOG(ERROR) << "Error closing zip file for fd " << dest_fd; | 260 DLOG(ERROR) << "Error closing zip file for fd " << dest_fd; |
220 success = false; | 261 success = false; |
221 } | 262 } |
222 | 263 |
223 return success; | 264 return success; |
224 } | 265 } |
225 #endif // defined(OS_POSIX) | 266 #endif // defined(OS_POSIX) |
226 | 267 |
227 } // namespace zip | 268 } // namespace zip |
OLD | NEW |