| 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" |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 87 | 87 |
| 88 bool ExcludeHiddenFilesFilter(const base::FilePath& file_path) { | 88 bool ExcludeHiddenFilesFilter(const base::FilePath& file_path) { |
| 89 return file_path.BaseName().value()[0] != '.'; | 89 return file_path.BaseName().value()[0] != '.'; |
| 90 } | 90 } |
| 91 | 91 |
| 92 } // namespace | 92 } // namespace |
| 93 | 93 |
| 94 namespace zip { | 94 namespace zip { |
| 95 | 95 |
| 96 bool Unzip(const base::FilePath& src_file, const base::FilePath& dest_dir) { | 96 bool Unzip(const base::FilePath& src_file, const base::FilePath& dest_dir) { |
| 97 return UnzipWithFilterCallback(src_file, dest_dir, |
| 98 base::Bind(&ExcludeNoFilesFilter)); |
| 99 } |
| 100 |
| 101 bool UnzipWithFilterCallback(const base::FilePath& src_file, |
| 102 const base::FilePath& dest_dir, |
| 103 const FilterCallback& filter_cb) { |
| 97 ZipReader reader; | 104 ZipReader reader; |
| 98 if (!reader.Open(src_file)) { | 105 if (!reader.Open(src_file)) { |
| 99 DLOG(WARNING) << "Failed to open " << src_file.value(); | 106 DLOG(WARNING) << "Failed to open " << src_file.value(); |
| 100 return false; | 107 return false; |
| 101 } | 108 } |
| 102 while (reader.HasMore()) { | 109 while (reader.HasMore()) { |
| 103 if (!reader.OpenCurrentEntryInZip()) { | 110 if (!reader.OpenCurrentEntryInZip()) { |
| 104 DLOG(WARNING) << "Failed to open the current file in zip"; | 111 DLOG(WARNING) << "Failed to open the current file in zip"; |
| 105 return false; | 112 return false; |
| 106 } | 113 } |
| 107 if (reader.current_entry_info()->is_unsafe()) { | 114 if (reader.current_entry_info()->is_unsafe()) { |
| 108 DLOG(WARNING) << "Found an unsafe file in zip " | 115 DLOG(WARNING) << "Found an unsafe file in zip " |
| 109 << reader.current_entry_info()->file_path().value(); | 116 << reader.current_entry_info()->file_path().value(); |
| 110 return false; | 117 return false; |
| 111 } | 118 } |
| 112 if (!reader.ExtractCurrentEntryIntoDirectory(dest_dir)) { | 119 if (filter_cb.Run(reader.current_entry_info()->file_path())) { |
| 113 DLOG(WARNING) << "Failed to extract " | 120 if (!reader.ExtractCurrentEntryIntoDirectory(dest_dir)) { |
| 121 DLOG(WARNING) << "Failed to extract " |
| 122 << reader.current_entry_info()->file_path().value(); |
| 123 return false; |
| 124 } |
| 125 } else { |
| 126 DLOG(WARNING) << "Skipped file " |
| 114 << reader.current_entry_info()->file_path().value(); | 127 << reader.current_entry_info()->file_path().value(); |
| 115 return false; | |
| 116 } | 128 } |
| 129 |
| 117 if (!reader.AdvanceToNextEntry()) { | 130 if (!reader.AdvanceToNextEntry()) { |
| 118 DLOG(WARNING) << "Failed to advance to the next file"; | 131 DLOG(WARNING) << "Failed to advance to the next file"; |
| 119 return false; | 132 return false; |
| 120 } | 133 } |
| 121 } | 134 } |
| 122 return true; | 135 return true; |
| 123 } | 136 } |
| 124 | 137 |
| 125 bool ZipWithFilterCallback(const base::FilePath& src_dir, | 138 bool ZipWithFilterCallback(const base::FilePath& src_dir, |
| 126 const base::FilePath& dest_file, | 139 const base::FilePath& dest_file, |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 196 if (ZIP_OK != zipClose(zip_file, NULL)) { | 209 if (ZIP_OK != zipClose(zip_file, NULL)) { |
| 197 DLOG(ERROR) << "Error closing zip file for fd " << dest_fd; | 210 DLOG(ERROR) << "Error closing zip file for fd " << dest_fd; |
| 198 success = false; | 211 success = false; |
| 199 } | 212 } |
| 200 | 213 |
| 201 return success; | 214 return success; |
| 202 } | 215 } |
| 203 #endif // defined(OS_POSIX) | 216 #endif // defined(OS_POSIX) |
| 204 | 217 |
| 205 } // namespace zip | 218 } // namespace zip |
| OLD | NEW |