diff options
author | grt <grt@chromium.org> | 2015-03-18 14:22:34 -0700 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2015-03-18 21:23:27 +0000 |
commit | ebc765ad54e4183149e3d5d859baa13a68ca1c6c (patch) | |
tree | 68c78cc05c4b7063886eeae4555b6bfe91808769 /google/zip_reader.h | |
parent | 8da8b87d318bc46d29ba32566d7e92ac32b1ed38 (diff) | |
download | zlib-ebc765ad54e4183149e3d5d859baa13a68ca1c6c.tar.gz |
Add ZipReader::ExtractCurrentEntry with a delegate interface.
This change gives consumers of ZipReader a way to have the current entry
streamed to them via a Delegate interface. It also:
- Reduces duplication in the ExtractCurrentEntry* functions.
- Uses the heap rather than the stack for intermediate buffers.
- Changes ExtractCurrentEntryToFd to ExtractCurrentEntryToFile, making
it cross-platform in the process.
BUG=462584
Committed: https://crrev.com/2919be01ff758875fc7161f6b41f4461518c1213
Cr-Commit-Position: refs/heads/master@{#320948}
Review URL: https://codereview.chromium.org/1014653002
Cr-Original-Commit-Position: refs/heads/master@{#321205}
Cr-Mirrored-From: https://chromium.googlesource.com/chromium/src
Cr-Mirrored-Commit: 98e6db00a6cc9cf6df682240d61f1b535b564cb8
Diffstat (limited to 'google/zip_reader.h')
-rw-r--r-- | google/zip_reader.h | 55 |
1 files changed, 48 insertions, 7 deletions
diff --git a/google/zip_reader.h b/google/zip_reader.h index 9280f23..da6cc93 100644 --- a/google/zip_reader.h +++ b/google/zip_reader.h @@ -23,6 +23,21 @@ namespace zip { +// A delegate interface used to stream out an entry; see +// ZipReader::ExtractCurrentEntry. +class WriterDelegate { + public: + virtual ~WriterDelegate() {} + + // Invoked once before any data is streamed out to pave the way (e.g., to open + // the output file). Return false on failure to cancel extraction. + virtual bool PrepareOutput() = 0; + + // Invoked to write the next chunk of data. Return false on failure to cancel + // extraction. + virtual bool WriteBytes(const char* data, int num_bytes) = 0; +}; + // This class is used for reading zip files. A typical use case of this // class is to scan entries in a zip file and extract them. The code will // look like: @@ -141,6 +156,9 @@ class ZipReader { // success. On failure, current_entry_info() becomes NULL. bool LocateAndOpenEntry(const base::FilePath& path_in_zip); + // Extracts the current entry in chunks to |delegate|. + bool ExtractCurrentEntry(WriterDelegate* delegate) const; + // Extracts the current entry to the given output file path. If the // current file is a directory, just creates a directory // instead. Returns true on success. OpenCurrentEntryInZip() must be @@ -148,7 +166,8 @@ class ZipReader { // // This function preserves the timestamp of the original entry. If that // timestamp is not valid, the timestamp will be set to the current time. - bool ExtractCurrentEntryToFilePath(const base::FilePath& output_file_path); + bool ExtractCurrentEntryToFilePath( + const base::FilePath& output_file_path) const; // Asynchronously extracts the current entry to the given output file path. // If the current entry is a directory it just creates the directory @@ -174,13 +193,11 @@ class ZipReader { // This function preserves the timestamp of the original entry. If that // timestamp is not valid, the timestamp will be set to the current time. bool ExtractCurrentEntryIntoDirectory( - const base::FilePath& output_directory_path); + const base::FilePath& output_directory_path) const; -#if defined(OS_POSIX) - // Extracts the current entry by writing directly to a file descriptor. - // Does not close the file descriptor. Returns true on success. - bool ExtractCurrentEntryToFd(int fd); -#endif + // Extracts the current entry by writing directly to a platform file. + // Does not close the file. Returns true on success. + bool ExtractCurrentEntryToFile(base::File* file) const; // Extracts the current entry into memory. If the current entry is a directory // the |output| parameter is set to the empty string. If the current entry is @@ -232,6 +249,30 @@ class ZipReader { DISALLOW_COPY_AND_ASSIGN(ZipReader); }; +// A writer delegate that writes to a given File. +class FileWriterDelegate : public WriterDelegate { + public: + explicit FileWriterDelegate(base::File* file); + + // Truncates the file to the number of bytes written. + ~FileWriterDelegate() override; + + // WriterDelegate methods: + + // Seeks to the beginning of the file, returning false if the seek fails. + bool PrepareOutput() override; + + // Writes |num_bytes| bytes of |data| to the file, returning false on error or + // if not all bytes could be written. + bool WriteBytes(const char* data, int num_bytes) override; + + private: + base::File* file_; + int64_t file_length_; + + DISALLOW_COPY_AND_ASSIGN(FileWriterDelegate); +}; + } // namespace zip #endif // THIRD_PARTY_ZLIB_GOOGLE_ZIP_READER_H_ |