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
3 // found in the LICENSE file.
5 #include "third_party/zlib/google/zip.h"
8 #include "base/file_util.h"
9 #include "base/files/file_enumerator.h"
10 #include "base/logging.h"
11 #include "base/strings/string16.h"
12 #include "base/strings/string_util.h"
13 #include "net/base/file_stream.h"
14 #include "third_party/zlib/google/zip_internal.h"
15 #include "third_party/zlib/google/zip_reader.h"
17 #if defined(USE_SYSTEM_MINIZIP)
18 #include <minizip/unzip.h>
19 #include <minizip/zip.h>
21 #include "third_party/zlib/contrib/minizip/unzip.h"
22 #include "third_party/zlib/contrib/minizip/zip.h"
27 bool AddFileToZip(zipFile zip_file, const base::FilePath& src_dir) {
28 net::FileStream stream(NULL);
29 int flags = base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_READ;
30 if (stream.OpenSync(src_dir, flags) != 0) {
31 DLOG(ERROR) << "Could not open stream for path "
37 char buf[zip::internal::kZipBufSize];
39 num_bytes = stream.ReadSync(buf, zip::internal::kZipBufSize);
41 if (ZIP_OK != zipWriteInFileInZip(zip_file, buf, num_bytes)) {
42 DLOG(ERROR) << "Could not write data to zip for path "
47 } while (num_bytes > 0);
52 bool AddEntryToZip(zipFile zip_file, const base::FilePath& path,
53 const base::FilePath& root_path) {
54 std::string str_path =
55 path.AsUTF8Unsafe().substr(root_path.AsUTF8Unsafe().length() + 1);
57 ReplaceSubstringsAfterOffset(&str_path, 0u, "\\", "/");
60 bool is_directory = base::DirectoryExists(path);
64 if (ZIP_OK != zipOpenNewFileInZip(
65 zip_file, str_path.c_str(),
66 NULL, NULL, 0u, NULL, 0u, NULL, // file info, extrafield local, length,
67 // extrafield global, length, comment
68 Z_DEFLATED, Z_DEFAULT_COMPRESSION)) {
69 DLOG(ERROR) << "Could not open zip file entry " << str_path;
75 success = AddFileToZip(zip_file, path);
78 if (ZIP_OK != zipCloseFileInZip(zip_file)) {
79 DLOG(ERROR) << "Could not close zip file entry " << str_path;
86 bool ExcludeNoFilesFilter(const base::FilePath& file_path) {
90 bool ExcludeHiddenFilesFilter(const base::FilePath& file_path) {
91 return file_path.BaseName().value()[0] != '.';
98 bool Unzip(const base::FilePath& src_file, const base::FilePath& dest_dir) {
100 if (!reader.Open(src_file)) {
101 DLOG(WARNING) << "Failed to open " << src_file.value();
104 while (reader.HasMore()) {
105 if (!reader.OpenCurrentEntryInZip()) {
106 DLOG(WARNING) << "Failed to open the current file in zip";
109 if (reader.current_entry_info()->is_unsafe()) {
110 DLOG(WARNING) << "Found an unsafe file in zip "
111 << reader.current_entry_info()->file_path().value();
114 if (!reader.ExtractCurrentEntryIntoDirectory(dest_dir)) {
115 DLOG(WARNING) << "Failed to extract "
116 << reader.current_entry_info()->file_path().value();
119 if (!reader.AdvanceToNextEntry()) {
120 DLOG(WARNING) << "Failed to advance to the next file";
127 bool ZipWithFilterCallback(const base::FilePath& src_dir,
128 const base::FilePath& dest_file,
129 const FilterCallback& filter_cb) {
130 DCHECK(base::DirectoryExists(src_dir));
132 zipFile zip_file = internal::OpenForZipping(dest_file.AsUTF8Unsafe(),
133 APPEND_STATUS_CREATE);
136 DLOG(WARNING) << "couldn't create file " << dest_file.value();
141 base::FileEnumerator file_enumerator(src_dir, true /* recursive */,
142 base::FileEnumerator::FILES | base::FileEnumerator::DIRECTORIES);
143 for (base::FilePath path = file_enumerator.Next(); !path.value().empty();
144 path = file_enumerator.Next()) {
145 if (!filter_cb.Run(path)) {
149 if (!AddEntryToZip(zip_file, path, src_dir)) {
155 if (ZIP_OK != zipClose(zip_file, NULL)) {
156 DLOG(ERROR) << "Error closing zip file " << dest_file.value();
163 bool Zip(const base::FilePath& src_dir, const base::FilePath& dest_file,
164 bool include_hidden_files) {
165 if (include_hidden_files) {
166 return ZipWithFilterCallback(
167 src_dir, dest_file, base::Bind(&ExcludeNoFilesFilter));
169 return ZipWithFilterCallback(
170 src_dir, dest_file, base::Bind(&ExcludeHiddenFilesFilter));
174 #if defined(OS_POSIX)
175 bool ZipFiles(const base::FilePath& src_dir,
176 const std::vector<base::FilePath>& src_relative_paths,
178 DCHECK(base::DirectoryExists(src_dir));
179 zipFile zip_file = internal::OpenFdForZipping(dest_fd, APPEND_STATUS_CREATE);
182 DLOG(ERROR) << "couldn't create file for fd " << dest_fd;
187 for (std::vector<base::FilePath>::const_iterator iter =
188 src_relative_paths.begin();
189 iter != src_relative_paths.end(); ++iter) {
190 const base::FilePath& path = src_dir.Append(*iter);
191 if (!AddEntryToZip(zip_file, path, src_dir)) {
192 // TODO(hshi): clean up the partial zip file when error occurs.
198 if (ZIP_OK != zipClose(zip_file, NULL)) {
199 DLOG(ERROR) << "Error closing zip file for fd " << dest_fd;
205 #endif // defined(OS_POSIX)