# HG changeset patch # User Sebastien Jodogne # Date 1422282134 -3600 # Node ID 46bca019587e1e60f79a71d0160270adc96b1dcc # Parent 6164f7200c433d5ff329bfcc6d6792b88693a474 primitives to add new content to existing ZIP files diff -r 6164f7200c43 -r 46bca019587e Core/Compression/HierarchicalZipWriter.h --- a/Core/Compression/HierarchicalZipWriter.h Fri Jan 23 14:42:33 2015 +0100 +++ b/Core/Compression/HierarchicalZipWriter.h Mon Jan 26 15:22:14 2015 +0100 @@ -114,6 +114,16 @@ return writer_.GetCompressionLevel(); } + void SetAppendToExisting(bool append) + { + writer_.SetAppendToExisting(append); + } + + bool IsAppendToExisting() const + { + return writer_.IsAppendToExisting(); + } + void OpenFile(const char* name); void OpenDirectory(const char* name); diff -r 6164f7200c43 -r 46bca019587e Core/Compression/ZipWriter.cpp --- a/Core/Compression/ZipWriter.cpp Fri Jan 23 14:42:33 2015 +0100 +++ b/Core/Compression/ZipWriter.cpp Mon Jan 26 15:22:14 2015 +0100 @@ -77,15 +77,19 @@ struct ZipWriter::PImpl { zipFile file_; + + PImpl() : file_(NULL) + { + } }; - ZipWriter::ZipWriter() : pimpl_(new PImpl) + ZipWriter::ZipWriter() : + pimpl_(new PImpl), + isZip64_(false), + hasFileInZip_(false), + append_(false), + compressionLevel_(6) { - compressionLevel_ = 6; - hasFileInZip_ = false; - isZip64_ = false; - - pimpl_->file_ = NULL; } ZipWriter::~ZipWriter() @@ -122,13 +126,15 @@ hasFileInZip_ = false; + int mode = (append_ ? APPEND_STATUS_ADDINZIP : APPEND_STATUS_CREATE); + if (isZip64_) { - pimpl_->file_ = zipOpen64(path_.c_str(), APPEND_STATUS_CREATE); + pimpl_->file_ = zipOpen64(path_.c_str(), mode); } else { - pimpl_->file_ = zipOpen(path_.c_str(), APPEND_STATUS_CREATE); + pimpl_->file_ = zipOpen(path_.c_str(), mode); } if (!pimpl_->file_) @@ -230,4 +236,13 @@ length -= bytes; } } + + + void ZipWriter::SetAppendToExisting(bool append) + { + Close(); + append_ = append; + } + + } diff -r 6164f7200c43 -r 46bca019587e Core/Compression/ZipWriter.h --- a/Core/Compression/ZipWriter.h Fri Jan 23 14:42:33 2015 +0100 +++ b/Core/Compression/ZipWriter.h Mon Jan 26 15:22:14 2015 +0100 @@ -50,6 +50,7 @@ bool isZip64_; bool hasFileInZip_; + bool append_; uint8_t compressionLevel_; std::string path_; @@ -71,6 +72,13 @@ { return compressionLevel_; } + + void SetAppendToExisting(bool append); + + bool IsAppendToExisting() const + { + return append_; + } void Open(); diff -r 6164f7200c43 -r 46bca019587e UnitTestsSources/ZipTests.cpp --- a/UnitTestsSources/ZipTests.cpp Fri Jan 23 14:42:33 2015 +0100 +++ b/UnitTestsSources/ZipTests.cpp Mon Jan 26 15:22:14 2015 +0100 @@ -72,6 +72,28 @@ } +TEST(ZipWriter, Append) +{ + { + Orthanc::ZipWriter w; + w.SetAppendToExisting(false); + w.SetOutputPath("UnitTestsResults/append.zip"); + w.Open(); + w.OpenFile("world/hello"); + w.Write("Hello world 1"); + } + + { + Orthanc::ZipWriter w; + w.SetAppendToExisting(true); + w.SetOutputPath("UnitTestsResults/append.zip"); + w.Open(); + w.OpenFile("world/appended"); + w.Write("Hello world 2"); + } +} + +