changeset 1277:46bca019587e

primitives to add new content to existing ZIP files
author Sebastien Jodogne <s.jodogne@gmail.com>
date Mon, 26 Jan 2015 15:22:14 +0100
parents 6164f7200c43
children 7aa0630a958e
files Core/Compression/HierarchicalZipWriter.h Core/Compression/ZipWriter.cpp Core/Compression/ZipWriter.h UnitTestsSources/ZipTests.cpp
diffstat 4 files changed, 63 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- 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);
--- 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;
+  }
+    
+
 }
--- 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();
 
--- 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");
+  }
+}
+
+