diff options
author | Adam Lesinski <adamlesinski@google.com> | 2017-03-16 13:23:51 -0700 |
---|---|---|
committer | Adam Lesinski <adamlesinski@google.com> | 2017-03-22 16:46:42 -0700 |
commit | 537713bace8975d02576225536ec55bd61f9ca91 (patch) | |
tree | 426a4efd55768e8085f760968365ed19c819e36c /include | |
parent | a4bc98941af87ba95bb72b8be59e74ad5686e7d8 (diff) | |
download | core-537713bace8975d02576225536ec55bd61f9ca91.tar.gz core-537713bace8975d02576225536ec55bd61f9ca91.tar.bz2 core-537713bace8975d02576225536ec55bd61f9ca91.zip |
libziparchive: Add ability to backup in ZipWriter
Based on the compressed size of a file entry,
the decision needs to be made to instead store the file
uncompressed. This adds support to ZipWriter to backup
its last file entry.
The file is now always truncated when the EOCD is written out,
to account for the case where a file entry is backed-up and the
resulting file written is much smaller, leaving garbage data at
the end of the file.
This change also includes a rename of FileInfo -> FileEntry.
This struct was private (now public), so it shouldn't affect any
clients.
Bug: 35461578
Test: make ziparchive-tests
Change-Id: I23dc584406274ab7b8ce62b3fbc3562ca4c2603e
Diffstat (limited to 'include')
-rw-r--r-- | include/ziparchive/zip_writer.h | 56 |
1 files changed, 37 insertions, 19 deletions
diff --git a/include/ziparchive/zip_writer.h b/include/ziparchive/zip_writer.h index 0b6ede45c..41ca2e1a3 100644 --- a/include/ziparchive/zip_writer.h +++ b/include/ziparchive/zip_writer.h @@ -17,15 +17,16 @@ #ifndef LIBZIPARCHIVE_ZIPWRITER_H_ #define LIBZIPARCHIVE_ZIPWRITER_H_ -#include "android-base/macros.h" -#include <utils/Compat.h> - #include <cstdio> #include <ctime> +#include <zlib.h> + #include <memory> #include <string> #include <vector> -#include <zlib.h> + +#include "android-base/macros.h" +#include "utils/Compat.h" /** * Writes a Zip file via a stateful interface. @@ -63,6 +64,20 @@ public: kAlign32 = 0x02, }; + /** + * A struct representing a zip file entry. + */ + struct FileEntry { + std::string path; + uint16_t compression_method; + uint32_t crc32; + uint32_t compressed_size; + uint32_t uncompressed_size; + uint16_t last_mod_time; + uint16_t last_mod_date; + uint32_t local_file_header_offset; + }; + static const char* ErrorCodeString(int32_t error_code); /** @@ -122,6 +137,19 @@ public: int32_t FinishEntry(); /** + * Discards the last-written entry. Can only be called after an entry has been written using + * FinishEntry(). + * Returns 0 on success, and an error value < 0 on failure. + */ + int32_t DiscardLastEntry(); + + /** + * Sets `out_entry` to the last entry written after a call to FinishEntry(). + * Returns 0 on success, and an error value < 0 if no entries have been written. + */ + int32_t GetLastEntry(FileEntry* out_entry); + + /** * Writes the Central Directory Headers and flushes the zip file stream. * Returns 0 on success, and an error value < 0 on failure. */ @@ -130,22 +158,11 @@ public: private: DISALLOW_COPY_AND_ASSIGN(ZipWriter); - struct FileInfo { - std::string path; - uint16_t compression_method; - uint32_t crc32; - uint32_t compressed_size; - uint32_t uncompressed_size; - uint16_t last_mod_time; - uint16_t last_mod_date; - uint32_t local_file_header_offset; - }; - int32_t HandleError(int32_t error_code); int32_t PrepareDeflate(); - int32_t StoreBytes(FileInfo* file, const void* data, size_t len); - int32_t CompressBytes(FileInfo* file, const void* data, size_t len); - int32_t FlushCompressedBytes(FileInfo* file); + int32_t StoreBytes(FileEntry* file, const void* data, size_t len); + int32_t CompressBytes(FileEntry* file, const void* data, size_t len); + int32_t FlushCompressedBytes(FileEntry* file); enum class State { kWritingZip, @@ -157,7 +174,8 @@ private: FILE* file_; off64_t current_offset_; State state_; - std::vector<FileInfo> files_; + std::vector<FileEntry> files_; + FileEntry current_file_entry_; std::unique_ptr<z_stream, void(*)(z_stream*)> z_stream_; std::vector<uint8_t> buffer_; |