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

Side by Side 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 unified diff | 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 »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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/bind.h" 7 #include "base/bind.h"
8 #include "base/files/file.h" 8 #include "base/files/file.h"
9 #include "base/logging.h" 9 #include "base/logging.h"
10 #include "base/message_loop/message_loop.h" 10 #include "base/message_loop/message_loop.h"
(...skipping 326 matching lines...) Expand 10 before | Expand all | Expand 10 after
337 break; 337 break;
338 } 338 }
339 } 339 }
340 } 340 }
341 341
342 unzCloseCurrentFile(zip_file_); 342 unzCloseCurrentFile(zip_file_);
343 return success; 343 return success;
344 } 344 }
345 #endif // defined(OS_POSIX) 345 #endif // defined(OS_POSIX)
346 346
347 bool ZipReader::ExtractCurrentEntryToString(
348 size_t max_read_bytes,
349 std::string* output) const {
350 DCHECK(output);
351 DCHECK(zip_file_);
352 DCHECK(max_read_bytes != 0);
353
354 if (current_entry_info()->is_directory()) {
355 output->clear();
356 return true;
357 }
358
359 const int open_result = unzOpenCurrentFile(zip_file_);
360 if (open_result != UNZ_OK)
361 return false;
362
363 // The original_size() is the best hint for the real size, so it saves
364 // doing reallocations for the common case when the uncompressed size is
365 // correct. However, we need to assume that the uncompressed size could be
366 // incorrect therefore this function needs to read as much data as possible.
367 std::string contents;
368 contents.reserve(std::min<size_t>(
369 max_read_bytes, current_entry_info()->original_size()));
370
371 bool success = true; // This becomes false when something bad happens.
372 char buf[internal::kZipBufSize];
373 while (true) {
374 const int num_bytes_read = unzReadCurrentFile(zip_file_, buf,
375 internal::kZipBufSize);
376 if (num_bytes_read == 0) {
377 // Reached the end of the file.
378 break;
379 } else if (num_bytes_read < 0) {
380 // If num_bytes_read < 0, then it's a specific UNZ_* error code.
381 success = false;
382 break;
383 } else if (num_bytes_read > 0) {
384 if (contents.size() + num_bytes_read > max_read_bytes) {
385 success = false;
386 break;
387 }
388 contents.append(buf, num_bytes_read);
389 }
390 }
391
392 unzCloseCurrentFile(zip_file_);
393 if (success)
394 output->swap(contents);
395
396 return success;
397 }
398
347 bool ZipReader::OpenInternal() { 399 bool ZipReader::OpenInternal() {
348 DCHECK(zip_file_); 400 DCHECK(zip_file_);
349 401
350 unz_global_info zip_info = {}; // Zero-clear. 402 unz_global_info zip_info = {}; // Zero-clear.
351 if (unzGetGlobalInfo(zip_file_, &zip_info) != UNZ_OK) { 403 if (unzGetGlobalInfo(zip_file_, &zip_info) != UNZ_OK) {
352 return false; 404 return false;
353 } 405 }
354 num_entries_ = zip_info.number_entry; 406 num_entries_ = zip_info.number_entry;
355 if (num_entries_ < 0) 407 if (num_entries_ < 0)
356 return false; 408 return false;
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
404 success_callback, 456 success_callback,
405 failure_callback, 457 failure_callback,
406 progress_callback, 458 progress_callback,
407 current_progress)); 459 current_progress));
408 460
409 } 461 }
410 } 462 }
411 463
412 464
413 } // namespace zip 465 } // namespace zip
OLDNEW
« 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