| 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_reader.h" | 5 #include "third_party/zlib/google/zip_reader.h" |
| 6 | 6 |
| 7 #include "base/file_util.h" | 7 #include "base/bind.h" |
| 8 #include "base/files/file.h" |
| 8 #include "base/logging.h" | 9 #include "base/logging.h" |
| 9 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
| 10 #include "base/strings/string_util.h" | 11 #include "base/strings/string_util.h" |
| 11 #include "base/strings/utf_string_conversions.h" | 12 #include "base/strings/utf_string_conversions.h" |
| 12 #include "net/base/file_stream.h" | |
| 13 #include "third_party/zlib/google/zip_internal.h" | 13 #include "third_party/zlib/google/zip_internal.h" |
| 14 | 14 |
| 15 #if defined(USE_SYSTEM_MINIZIP) | 15 #if defined(USE_SYSTEM_MINIZIP) |
| 16 #include <minizip/unzip.h> | 16 #include <minizip/unzip.h> |
| 17 #else | 17 #else |
| 18 #include "third_party/zlib/contrib/minizip/unzip.h" | 18 #include "third_party/zlib/contrib/minizip/unzip.h" |
| 19 #if defined(OS_WIN) | 19 #if defined(OS_WIN) |
| 20 #include "third_party/zlib/contrib/minizip/iowin32.h" | 20 #include "third_party/zlib/contrib/minizip/iowin32.h" |
| 21 #endif // defined(OS_WIN) | 21 #endif // defined(OS_WIN) |
| 22 #endif // defined(USE_SYSTEM_MINIZIP) | 22 #endif // defined(USE_SYSTEM_MINIZIP) |
| (...skipping 175 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 198 const int open_result = unzOpenCurrentFile(zip_file_); | 198 const int open_result = unzOpenCurrentFile(zip_file_); |
| 199 if (open_result != UNZ_OK) | 199 if (open_result != UNZ_OK) |
| 200 return false; | 200 return false; |
| 201 | 201 |
| 202 // We can't rely on parent directory entries being specified in the | 202 // We can't rely on parent directory entries being specified in the |
| 203 // zip, so we make sure they are created. | 203 // zip, so we make sure they are created. |
| 204 base::FilePath output_dir_path = output_file_path.DirName(); | 204 base::FilePath output_dir_path = output_file_path.DirName(); |
| 205 if (!base::CreateDirectory(output_dir_path)) | 205 if (!base::CreateDirectory(output_dir_path)) |
| 206 return false; | 206 return false; |
| 207 | 207 |
| 208 net::FileStream stream(NULL); | 208 base::File file(output_file_path, |
| 209 const int flags = (base::PLATFORM_FILE_CREATE_ALWAYS | | 209 base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_WRITE); |
| 210 base::PLATFORM_FILE_WRITE); | 210 if (!file.IsValid()) |
| 211 if (stream.OpenSync(output_file_path, flags) != 0) | |
| 212 return false; | 211 return false; |
| 213 | 212 |
| 214 bool success = true; // This becomes false when something bad happens. | 213 bool success = true; // This becomes false when something bad happens. |
| 215 while (true) { | 214 while (true) { |
| 216 char buf[internal::kZipBufSize]; | 215 char buf[internal::kZipBufSize]; |
| 217 const int num_bytes_read = unzReadCurrentFile(zip_file_, buf, | 216 const int num_bytes_read = unzReadCurrentFile(zip_file_, buf, |
| 218 internal::kZipBufSize); | 217 internal::kZipBufSize); |
| 219 if (num_bytes_read == 0) { | 218 if (num_bytes_read == 0) { |
| 220 // Reached the end of the file. | 219 // Reached the end of the file. |
| 221 break; | 220 break; |
| 222 } else if (num_bytes_read < 0) { | 221 } else if (num_bytes_read < 0) { |
| 223 // If num_bytes_read < 0, then it's a specific UNZ_* error code. | 222 // If num_bytes_read < 0, then it's a specific UNZ_* error code. |
| 224 success = false; | 223 success = false; |
| 225 break; | 224 break; |
| 226 } else if (num_bytes_read > 0) { | 225 } else if (num_bytes_read > 0) { |
| 227 // Some data is read. Write it to the output file. | 226 // Some data is read. Write it to the output file. |
| 228 if (num_bytes_read != stream.WriteSync(buf, num_bytes_read)) { | 227 if (num_bytes_read != file.WriteAtCurrentPos(buf, num_bytes_read)) { |
| 229 success = false; | 228 success = false; |
| 230 break; | 229 break; |
| 231 } | 230 } |
| 232 } | 231 } |
| 233 } | 232 } |
| 234 | 233 |
| 235 stream.CloseSync(); | 234 file.Close(); |
| 236 unzCloseCurrentFile(zip_file_); | 235 unzCloseCurrentFile(zip_file_); |
| 237 | 236 |
| 238 if (current_entry_info()->last_modified() != base::Time::UnixEpoch()) | 237 if (current_entry_info()->last_modified() != base::Time::UnixEpoch()) |
| 239 base::TouchFile(output_file_path, | 238 base::TouchFile(output_file_path, |
| 240 base::Time::Now(), | 239 base::Time::Now(), |
| 241 current_entry_info()->last_modified()); | 240 current_entry_info()->last_modified()); |
| 242 | 241 |
| 243 return success; | 242 return success; |
| 244 } | 243 } |
| 245 | 244 |
| (...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 405 success_callback, | 404 success_callback, |
| 406 failure_callback, | 405 failure_callback, |
| 407 progress_callback, | 406 progress_callback, |
| 408 current_progress)); | 407 current_progress)); |
| 409 | 408 |
| 410 } | 409 } |
| 411 } | 410 } |
| 412 | 411 |
| 413 | 412 |
| 414 } // namespace zip | 413 } // namespace zip |
| OLD | NEW |