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 ff5a39514af113d28f5266d2c119d51a4d386328..2564f8d1a2f010219faf2a4fdc16326081a41719 100644 |
--- a/third_party/zlib/google/zip_reader.cc |
+++ b/third_party/zlib/google/zip_reader.cc |
@@ -325,6 +325,47 @@ bool ZipReader::ExtractCurrentEntry(WriterDelegate* delegate) const { |
return success; |
} |
+bool ZipReader::ExtractPartOfCurrentEntry(WriterDelegate* delegate, |
+ size_t num_bytes) const { |
+ DCHECK(zip_file_); |
+ |
+ const int open_result = unzOpenCurrentFile(zip_file_); |
+ if (open_result != UNZ_OK) |
+ return false; |
+ |
+ if (!delegate->PrepareOutput()) |
+ return false; |
+ |
+ bool success = true; // This becomes false when something bad happens. |
+ std::unique_ptr<char[]> buf(new char[num_bytes]); |
+ size_t total_num_bytes_read = 0; |
+ while (true) { |
+ const int num_bytes_read = unzReadCurrentFile( |
+ zip_file_, buf.get(), base::checked_cast<unsigned int>(num_bytes)); |
+ 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. |
+ if (!delegate->WriteBytes(buf.get(), num_bytes_read)) { |
+ success = false; |
+ break; |
+ } |
+ } |
+ // Previous check ensures that num_bytes_read is not negative here. |
+ total_num_bytes_read += base::checked_cast<size_t>(num_bytes_read); |
+ if (total_num_bytes_read == num_bytes) { |
+ // Read all the data. |
+ break; |
+ } |
+ } |
+ |
+ unzCloseCurrentFile(zip_file_); |
+ |
+ return success; |
+} |
+ |
bool ZipReader::ExtractCurrentEntryToFilePath( |
const base::FilePath& output_file_path) const { |
DCHECK(zip_file_); |
@@ -446,6 +487,27 @@ bool ZipReader::ExtractCurrentEntryToString(size_t max_read_bytes, |
return true; |
} |
+bool ZipReader::ExtractPartOfCurrentEntryToString(size_t num_bytes, |
+ std::string* output) const { |
+ DCHECK(output); |
+ DCHECK(zip_file_); |
+ DCHECK_NE(0U, num_bytes); |
+ |
+ if (current_entry_info()->is_directory()) { |
+ output->clear(); |
+ return true; |
+ } |
+ |
+ std::string contents; |
+ contents.reserve(num_bytes); |
+ |
+ StringWriterDelegate writer(num_bytes, &contents); |
+ if (!ExtractPartOfCurrentEntry(&writer, num_bytes)) |
+ return false; |
+ output->swap(contents); |
+ return true; |
+} |
+ |
bool ZipReader::OpenInternal() { |
DCHECK(zip_file_); |