15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2011 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
4c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#ifndef THIRD_PARTY_ZLIB_GOOGLE_ZIP_READER_H_
5c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#define THIRD_PARTY_ZLIB_GOOGLE_ZIP_READER_H_
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <string>
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/basictypes.h"
105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "base/callback.h"
11a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "base/files/file.h"
122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/files/file_path.h"
131320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "base/files/file_util.h"
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/memory/scoped_ptr.h"
155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "base/memory/weak_ptr.h"
16eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "base/time/time.h"
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if defined(USE_SYSTEM_MINIZIP)
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <minizip/unzip.h>
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#else
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "third_party/zlib/contrib/minizip/unzip.h"
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace zip {
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// This class is used for reading zip files. A typical use case of this
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// class is to scan entries in a zip file and extract them. The code will
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// look like:
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//   ZipReader reader;
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//   reader.Open(zip_file_path);
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//   while (reader.HasMore()) {
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//     reader.OpenCurrentEntryInZip();
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//     reader.ExtractCurrentEntryToDirectory(output_directory_path);
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//     reader.AdvanceToNextEntry();
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//   }
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
386d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)// For simplicity, error checking is omitted in the example code above. The
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// production code should check return values from all of these functions.
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// This calls can also be used for random access of contents in a zip file
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// using LocateAndOpenEntry().
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class ZipReader {
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
465d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // A callback that is called when the operation is successful.
475d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  typedef base::Closure SuccessCallback;
485d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // A callback that is called when the operation fails.
495d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  typedef base::Closure FailureCallback;
505d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // A callback that is called periodically during the operation with the number
515d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // of bytes that have been processed so far.
525d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  typedef base::Callback<void(int64)> ProgressCallback;
535d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // This class represents information of an entry (file or directory) in
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // a zip file.
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  class EntryInfo {
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   public:
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    EntryInfo(const std::string& filename_in_zip,
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)              const unz_file_info& raw_file_info);
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Returns the file path. The path is usually relative like
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // "foo/bar.txt", but if it's absolute, is_unsafe() returns true.
632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const base::FilePath& file_path() const { return file_path_; }
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Returns the size of the original file (i.e. after uncompressed).
665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Returns 0 if the entry is a directory.
67010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    // Note: this value should not be trusted, because it is stored as metadata
68010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    // in the zip archive and can be different from the real uncompressed size.
695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    int64 original_size() const { return original_size_; }
705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
715d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    // Returns the last modified time. If the time stored in the zip file was
725d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    // not valid, the unix epoch will be returned.
735d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    //
745d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    // The time stored in the zip archive uses the MS-DOS date and time format.
755d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    // http://msdn.microsoft.com/en-us/library/ms724247(v=vs.85).aspx
765d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    // As such the following limitations apply:
775d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    // * only years from 1980 to 2107 can be represented.
785d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    // * the time stamp has a 2 second resolution.
795d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    // * there's no timezone information, so the time is interpreted as local.
805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    base::Time last_modified() const { return last_modified_; }
815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Returns true if the entry is a directory.
835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    bool is_directory() const { return is_directory_; }
845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Returns true if the entry is unsafe, like having ".." or invalid
865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // UTF-8 characters in its file name, or the file path is absolute.
875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    bool is_unsafe() const { return is_unsafe_; }
885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   private:
902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const base::FilePath file_path_;
915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    int64 original_size_;
925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    base::Time last_modified_;
935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    bool is_directory_;
945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    bool is_unsafe_;
955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    DISALLOW_COPY_AND_ASSIGN(EntryInfo);
965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  };
975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ZipReader();
995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ~ZipReader();
1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Opens the zip file specified by |zip_file_path|. Returns true on
1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // success.
1032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  bool Open(const base::FilePath& zip_file_path);
1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1052a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Opens the zip file referred to by the platform file |zip_fd|.
1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns true on success.
1072a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  bool OpenFromPlatformFile(base::PlatformFile zip_fd);
1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Opens the zip data stored in |data|. This class uses a weak reference to
1105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // the given sring while extracting files, i.e. the caller should keep the
1115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // string until it finishes extracting files.
1125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool OpenFromString(const std::string& data);
1135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Closes the currently opened zip file. This function is called in the
1155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // destructor of the class, so you usually don't need to call this.
1165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void Close();
1175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns true if there is at least one entry to read. This function is
1195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // used to scan entries with AdvanceToNextEntry(), like:
1205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //
1215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // while (reader.HasMore()) {
1225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //   // Do something with the current file here.
1235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //   reader.AdvanceToNextEntry();
1245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // }
1255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool HasMore();
1265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Advances the next entry. Returns true on success.
1285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool AdvanceToNextEntry();
1295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Opens the current entry in the zip file. On success, returns true and
1315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // updates the the current entry state (i.e. current_entry_info() is
1325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // updated). This function should be called before operations over the
1335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // current entry like ExtractCurrentEntryToFile().
1345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //
1355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Note that there is no CloseCurrentEntryInZip(). The the current entry
1365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // state is reset automatically as needed.
1375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool OpenCurrentEntryInZip();
1385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Locates an entry in the zip file and opens it. Returns true on
1405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // success. This function internally calls OpenCurrentEntryInZip() on
1415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // success. On failure, current_entry_info() becomes NULL.
1422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  bool LocateAndOpenEntry(const base::FilePath& path_in_zip);
1435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Extracts the current entry to the given output file path. If the
1455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // current file is a directory, just creates a directory
1465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // instead. Returns true on success. OpenCurrentEntryInZip() must be
1475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // called beforehand.
1485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //
1495d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // This function preserves the timestamp of the original entry. If that
1505d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // timestamp is not valid, the timestamp will be set to the current time.
1512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  bool ExtractCurrentEntryToFilePath(const base::FilePath& output_file_path);
1525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1535d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Asynchronously extracts the current entry to the given output file path.
1545d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // If the current entry is a directory it just creates the directory
1555d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // synchronously instead.  OpenCurrentEntryInZip() must be called beforehand.
1565d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // success_callback will be called on success and failure_callback will be
1575d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // called on failure.  progress_callback will be called at least once.
1585d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Callbacks will be posted to the current MessageLoop in-order.
1595d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  void ExtractCurrentEntryToFilePathAsync(
1605d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      const base::FilePath& output_file_path,
1615d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      const SuccessCallback& success_callback,
1625d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      const FailureCallback& failure_callback,
1635d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      const ProgressCallback& progress_callback);
1645d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Extracts the current entry to the given output directory path using
1665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // ExtractCurrentEntryToFilePath(). Sub directories are created as needed
1675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // based on the file path of the current entry. For example, if the file
1685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // path in zip is "foo/bar.txt", and the output directory is "output",
1695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // "output/foo/bar.txt" will be created.
1705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //
1715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns true on success. OpenCurrentEntryInZip() must be called
1725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // beforehand.
1735d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  //
1745d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // This function preserves the timestamp of the original entry. If that
1755d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // timestamp is not valid, the timestamp will be set to the current time.
1762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  bool ExtractCurrentEntryIntoDirectory(
1772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      const base::FilePath& output_directory_path);
1785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if defined(OS_POSIX)
1805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Extracts the current entry by writing directly to a file descriptor.
1815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Does not close the file descriptor. Returns true on success.
1825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool ExtractCurrentEntryToFd(int fd);
1835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
1845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1856d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  // Extracts the current entry into memory. If the current entry is a directory
1866d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  // the |output| parameter is set to the empty string. If the current entry is
1876d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  // a file, the |output| parameter is filled with its contents. Returns true on
1886d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  // success. OpenCurrentEntryInZip() must be called beforehand.
1896d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  // Note: the |output| parameter can be filled with a big amount of data, avoid
1906d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  // passing it around by value, but by reference or pointer.
1916d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  // Note: the value returned by EntryInfo::original_size() cannot be
1926d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  // trusted, so the real size of the uncompressed contents can be different.
1936d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  // Use max_read_bytes to limit the ammount of memory used to carry the entry.
1946d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  // If the real size of the uncompressed data is bigger than max_read_bytes
1956d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  // then false is returned. |max_read_bytes| must be non-zero.
1966d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  bool ExtractCurrentEntryToString(
1976d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)      size_t max_read_bytes,
1986d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)      std::string* output) const;
1996d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)
2005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns the current entry info. Returns NULL if the current entry is
2015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // not yet opened. OpenCurrentEntryInZip() must be called beforehand.
2025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EntryInfo* current_entry_info() const {
2035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return current_entry_info_.get();
2045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
2055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns the number of entries in the zip file.
2075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Open() must be called beforehand.
2085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int num_entries() const { return num_entries_; }
2095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private:
2115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Common code used both in Open and OpenFromFd.
2125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool OpenInternal();
2135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Resets the internal state.
2155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void Reset();
2165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Extracts a chunk of the file to the target.  Will post a task for the next
2185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // chunk and success/failure/progress callbacks as necessary.
219a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  void ExtractChunk(base::File target_file,
2205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                    const SuccessCallback& success_callback,
2215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                    const FailureCallback& failure_callback,
2225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                    const ProgressCallback& progress_callback,
2235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                    const int64 offset);
2245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  unzFile zip_file_;
2265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int num_entries_;
2275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool reached_end_;
2285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  scoped_ptr<EntryInfo> current_entry_info_;
2295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  base::WeakPtrFactory<ZipReader> weak_ptr_factory_;
2315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(ZipReader);
2335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
2345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace zip
2365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
237c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#endif  // THIRD_PARTY_ZLIB_GOOGLE_ZIP_READER_H_
238