Updating a zip file's compression level after it's already created

Jun 25, 2010 at 7:07 PM

I have a zip file that has been created with no compression.

I would like to update the compression level without having to extract all of the files.  Is there a method of doing this?

I tried to simply read the zip file and then change the compression level and save the zip, with no luck:

 

ZipFile zip2 = ZipFile.Read(@"c:\users\daniel.judge.ACTIOFFICE\Desktop\Desktop.gt3x");
zip2.CompressionLevel = Ionic.Zlib.CompressionLevel.BestCompression;
zip2.Save();

Any suggestions?

And btw, your software works wonderfully.  We require the use of reading out compressed files byte by byte and it couldn't be simpler.  Thanks again!

 

Coordinator
Jun 28, 2010 at 8:01 PM

You should be able to do what you want, but you need to set the CompressionLevel property on the individual ZipEntry instances, not on the containing ZipFile. 

The way it works is that the CompressionLevel applies to entries, not to the entire zipfile as a whole.  So when you set the CompressionLevel on the ZipFile, this is the level that is ued for all subsequently added ZipEntries.  If you then call AddEntry(), the newly added entry will get the CompressionLevel from the ZipFile container. 

For updating a zip file, you'd need to set the compression level on each entry, then re-save the ZipFile.  

using (var zip = ZipFile.Read("c:\\myzip.zip"))
{
  foreach(var e in zip) 
  {
    e.CompressionLevel = CompressionLevel.Best;
  }
  zip.Save();
}