Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1021)

Unified Diff: third_party/zlib/google/zip_reader.cc

Issue 292443006: New ZipReader::ExtractCurrentEntryToString API. (Closed) Base URL: https://chromium.googlesource.com/chromium/src
Patch Set: rebase Created 6 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « third_party/zlib/google/zip_reader.h ('k') | third_party/zlib/google/zip_reader_unittest.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 aa85fa3baf22c34035a96b7aa9c24e4e130eb1ee..80b60b3166c629ef3700f226a8c8f4be97089bad 100644
--- a/third_party/zlib/google/zip_reader.cc
+++ b/third_party/zlib/google/zip_reader.cc
@@ -344,6 +344,58 @@ 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()));
+
+ bool success = true; // This becomes false when something bad happens.
+ char buf[internal::kZipBufSize];
+ while (true) {
+ 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) {
+ if (contents.size() + num_bytes_read > max_read_bytes) {
+ success = false;
+ break;
+ }
+ contents.append(buf, num_bytes_read);
+ }
+ }
+
+ unzCloseCurrentFile(zip_file_);
+ if (success)
+ output->swap(contents);
+
+ return success;
+}
+
bool ZipReader::OpenInternal() {
DCHECK(zip_file_);
« no previous file with comments | « third_party/zlib/google/zip_reader.h ('k') | third_party/zlib/google/zip_reader_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698