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

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: Created 6 years, 7 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();
satorux1 2014/05/23 23:45:47 i think we should always call output->clear() even
João Eiras 2014/06/02 16:41:07 It's my personal style to avoid changing output pa
satorux1 2014/06/03 08:17:40 That's fine by me.
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()) + 1);
satorux1 2014/05/23 23:45:47 i think we don't need +1
João Eiras 2014/06/02 16:41:07 Was there for unzReadCurrentFile() to tell if it h
370
371 size_t buffer_index = 0;
372 bool success = true;
373 while (true) {
374 // This just sets size() to be equal to capacity() so the chars which are re ad
satorux1 2014/05/23 23:45:47 line should be < 80 chars
375 // ahead will not be erased by calling resize() later.
376 contents.resize(contents.capacity());
satorux1 2014/05/23 23:45:47 hmm, this looks complex. I understand that you are
João Eiras 2014/06/02 16:41:07 It does look a bit overly complicated, but then my
377
378 const size_t space_available = contents.capacity() - buffer_index;
379 const int bytes_to_read = std::min<int>(INT_MAX, space_available);
380 const int bytes_read = unzReadCurrentFile(
381 zip_file_,
382 &(contents[buffer_index]),
383 bytes_to_read);
384 DCHECK(bytes_read <= bytes_to_read);
385
386 if (bytes_read == 0) {
387 contents.resize(buffer_index);
388 // Reached the end of the file.
389 break;
390 } else if (bytes_read < 0) {
391 // If num_bytes_read < 0, then it's a specific UNZ_* error code.
392 success = false;
393 break;
394 } else { // if (num_bytes_read > 0)
395 buffer_index += bytes_read;
396
397 if (buffer_index > max_read_bytes) {
398 success = false;
399 break;
400 }
401
402 if (bytes_read == bytes_to_read) {
403 // Filled up the buffer, no more space left, need to resize.
404 if (static_cast<size_t>(bytes_to_read) == space_available)
405 contents.reserve(contents.capacity() + internal::kZipBufSize);
406 } else {
407 contents.resize(buffer_index);
408 // Read less bytes than asked. That means it reached end of file.
409 break;
410 }
411 }
412 }
413
414 unzCloseCurrentFile(zip_file_);
415 if (success)
416 output->swap(contents);
417
418 return success;
419 }
420
347 bool ZipReader::OpenInternal() { 421 bool ZipReader::OpenInternal() {
348 DCHECK(zip_file_); 422 DCHECK(zip_file_);
349 423
350 unz_global_info zip_info = {}; // Zero-clear. 424 unz_global_info zip_info = {}; // Zero-clear.
351 if (unzGetGlobalInfo(zip_file_, &zip_info) != UNZ_OK) { 425 if (unzGetGlobalInfo(zip_file_, &zip_info) != UNZ_OK) {
352 return false; 426 return false;
353 } 427 }
354 num_entries_ = zip_info.number_entry; 428 num_entries_ = zip_info.number_entry;
355 if (num_entries_ < 0) 429 if (num_entries_ < 0)
356 return false; 430 return false;
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
404 success_callback, 478 success_callback,
405 failure_callback, 479 failure_callback,
406 progress_callback, 480 progress_callback,
407 current_progress)); 481 current_progress));
408 482
409 } 483 }
410 } 484 }
411 485
412 486
413 } // namespace zip 487 } // 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