Chromium Code Reviews| Index: chrome/common/zip_reader.cc |
| diff --git a/chrome/common/zip_reader.cc b/chrome/common/zip_reader.cc |
| index 88d3f8f936f0f384bbf3864d2df75112b36a9cb9..00254950fa62926c031dcecc74aad1bdb3f9470c 100644 |
| --- a/chrome/common/zip_reader.cc |
| +++ b/chrome/common/zip_reader.cc |
| @@ -82,18 +82,21 @@ bool ZipReader::Open(const FilePath& zip_file_path) { |
| return false; |
| } |
| - unz_global_info zip_info = {}; // Zero-clear. |
| - if (unzGetGlobalInfo(zip_file_, &zip_info) != UNZ_OK) { |
| + return Initialize(); |
|
satorux1
2011/12/12 04:54:14
OpenInternal() may sound a bit better?
Jorge Lucangeli Obes
2011/12/12 23:34:35
Done.
|
| +} |
| + |
| +#if defined(OS_POSIX) |
| +bool ZipReader::OpenFd(const int zip_fd) { |
| + DCHECK(!zip_file_); |
| + |
| + zip_file_ = internal::OpenFdForUnzipping(zip_fd); |
| + if (!zip_file_) { |
| return false; |
| } |
| - num_entries_ = zip_info.number_entry; |
| - if (num_entries_ < 0) |
| - return false; |
| - // We are already at the end if the zip file is empty. |
| - reached_end_ = (num_entries_ == 0); |
| - return true; |
| + return Initialize(); |
| } |
| +#endif |
| void ZipReader::Close() { |
| if (zip_file_) { |
| @@ -229,6 +232,62 @@ bool ZipReader::ExtractCurrentEntryIntoDirectory( |
| return ExtractCurrentEntryToFilePath(output_file_path); |
| } |
| +#if defined(OS_POSIX) |
| +bool ZipReader::ExtractCurrentEntryToFd(const int fd) { |
| + DCHECK(zip_file_); |
| + |
| + // If this is a directory, there's nothing to extract to the file descriptor, |
| + // so return false. |
| + if (current_entry_info()->is_directory()) |
| + return false; |
| + |
| + const int open_result = unzOpenCurrentFile(zip_file_); |
| + if (open_result != UNZ_OK) |
| + return false; |
| + |
| + bool success = true; // This becomes false when something bad happens. |
| + while (true) { |
| + char buf[internal::kZipBufSize]; |
| + const int num_bytes_read = unzReadCurrentFile(zip_file_, buf, |
| + internal::kZipBufSize); |
| + if (num_bytes_read == 0) { |
| + // Reached the end of the file. |
| + break; |
| + } else if (num_bytes_read < 0) { |
| + // If num_bytes_read < 0, then it's a specific UNZ_* error code. |
| + success = false; |
| + break; |
| + } else if (num_bytes_read > 0) { |
| + // Some data is read. Write it to the output file descriptor. |
| + if (num_bytes_read != |
| + file_util::WriteFileDescriptor(fd, buf, num_bytes_read)) { |
| + success = false; |
| + break; |
| + } |
| + } |
| + } |
| + |
| + unzCloseCurrentFile(zip_file_); |
| + return success; |
| +} |
| +#endif // defined(OS_POSIX) |
| + |
| +bool ZipReader::Initialize() { |
| + DCHECK(zip_file_); |
| + |
| + unz_global_info zip_info = {}; // Zero-clear. |
| + if (unzGetGlobalInfo(zip_file_, &zip_info) != UNZ_OK) { |
| + return false; |
| + } |
| + num_entries_ = zip_info.number_entry; |
| + if (num_entries_ < 0) |
| + return false; |
| + |
| + // We are already at the end if the zip file is empty. |
| + reached_end_ = (num_entries_ == 0); |
| + return true; |
| +} |
| + |
| void ZipReader::Reset() { |
| zip_file_ = NULL; |
| num_entries_ = 0; |