Index: third_party/zlib/google/zip_reader.cc |
diff --git a/third_party/zlib/google/zip_reader.cc b/third_party/zlib/google/zip_reader.cc |
index 7b7870ee981daf4287255dc50475890f094901e7..21304c8f431f797d384ee87405d1f0272159e00f 100644 |
--- a/third_party/zlib/google/zip_reader.cc |
+++ b/third_party/zlib/google/zip_reader.cc |
@@ -107,7 +107,7 @@ bool ZipReader::OpenFromPlatformFile(base::PlatformFile zip_fd) { |
} |
bool ZipReader::OpenFromString(const std::string& data) { |
- zip_file_ = internal::PreprareMemoryForUnzipping(data); |
+ zip_file_ = internal::PrepareMemoryForUnzipping(data); |
if (!zip_file_) |
return false; |
return OpenInternal(); |
@@ -351,6 +351,80 @@ bool ZipReader::ExtractCurrentEntryToFd(const int fd) { |
} |
#endif // defined(OS_POSIX) |
+bool ZipReader::ExtractCurrentEntryToString( |
+ size_t max_read_bytes, |
+ std::string* output) const { |
+ DCHECK(output); |
+ DCHECK(zip_file_); |
+ DCHECK(max_read_bytes != 0); |
+ |
+ if (current_entry_info()->is_directory()) { |
+ output->clear(); |
+ return true; |
+ } |
+ |
+ const int open_result = unzOpenCurrentFile(zip_file_); |
+ if (open_result != UNZ_OK) |
+ return false; |
+ |
+ // The original_size() is the best hint for the real size, so it saves |
+ // doing reallocations for the common case when the uncompressed size is |
+ // correct. However, we need to assume that the uncompressed size could be |
+ // incorrect therefore this function needs to read as much data as possible. |
+ std::string contents; |
+ contents.reserve(std::min<size_t>( |
+ max_read_bytes, current_entry_info()->original_size()) + 1); |
+ |
+ size_t buffer_index = 0; |
+ bool success = true; |
+ while (true) { |
+ // This just sets size() to be equal to capacity() so the chars which are read |
+ // ahead will not be erased by calling resize() later. |
+ contents.resize(contents.capacity()); |
+ |
+ const size_t bytes_to_read = contents.capacity() - buffer_index; |
+ const int bytes_to_read_really = std::min<int>(INT_MAX, bytes_to_read); |
+ const int bytes_read = unzReadCurrentFile( |
+ zip_file_, |
+ &(contents[buffer_index]), |
+ bytes_to_read_really); |
+ DCHECK(bytes_read <= bytes_to_read_really); |
+ |
+ if (bytes_read == 0) { |
+ contents.resize(buffer_index); |
+ // Reached the end of the file. |
+ break; |
+ } else if (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) |
+ buffer_index += bytes_read; |
+ |
+ if (buffer_index > max_read_bytes) { |
+ success = false; |
+ break; |
+ } |
+ |
+ if (bytes_read == bytes_to_read_really) { |
+ // Filled up the buffer, no more space left, need to resize. |
+ if (static_cast<size_t>(bytes_to_read_really) == bytes_to_read) |
+ contents.reserve(contents.capacity() + internal::kZipBufSize); |
+ } else { |
+ contents.resize(buffer_index); |
+ // Read less bytes than asked. That means it reached end of file. |
+ break; |
+ } |
+ } |
+ } |
+ |
+ unzCloseCurrentFile(zip_file_); |
+ if (success) |
+ output->swap(contents); |
+ |
+ return success; |
+} |
+ |
bool ZipReader::OpenInternal() { |
DCHECK(zip_file_); |