3

I am using this routine to create a Zipfile from an existing directory. It compiles and doesn't throw a run time error. However it doesn't do the task when it comes to reducing the size.

using Ionic.Zip;

//..

using (ZipFile zip = new ZipFile())

{
    zip.AddDirectory(SourceFolder);

    zip.Save(DestinationFolder + FolderName + ".zip");
}

Original folder has PNG images, one .xlsx file and one .xml file and the zipped folder always has the same size (not exactly equal but very similar). What are the possible reasons? Bad library?

These folders get generated constantly. Thus, taking a lot of space on the disk and we need to check/clean on daily basis. What can I do to reduce that?

Khalil Khalaf
  • 9,259
  • 11
  • 62
  • 104
  • 4
    Well if most of the data is already-compressed images, that's entirely expected. What happens if you just run a regular zip tool over the same set of files? – Jon Skeet Apr 18 '16 at 13:40
  • you cann't zip image and xlsx is already compressed – Amine Apr 18 '16 at 13:42
  • Why the down vote? – Khalil Khalaf Apr 18 '16 at 13:43
  • @jonskeet I tried a regular zip tool and I got the same result. I see it now. So anything I can do to compress that? Even with any lossy method? I don't care about the quality of the images at that point – Khalil Khalaf Apr 18 '16 at 13:44
  • To reduce the image you would have to use a tool like paint and select a different image quality. It can't be done with zip. – jdweng Apr 18 '16 at 13:50
  • @jdweng so code-wise, I need to convert the images or resize them and this is the only solution? – Khalil Khalaf Apr 18 '16 at 13:53
  • Simply put, you cannot reduce the size of *that* folder since it already contains compressed files, except for the .xml file that is. The savings gained from compressing the .xml file may or may not be worth it, but the time cost of trying to compress a .png and .xlsx file again will probably not be. Simply put, there is nothing to be gained here. Buy a bigger disk perhaps? – Lasse V. Karlsen Apr 18 '16 at 13:58
  • Yes. Binary image data size can't be decrease by zip. The number of ones and zeroes in a binary file is random. Zip looks for repeating ones and zeroes to do reduction. An image in most cases doesn't have repeating patterns to reduce. – jdweng Apr 18 '16 at 14:00
  • What type of picutures are used? Real fotographs or more diagrams with large area of colour – Schafwolle Apr 18 '16 at 14:06

2 Answers2

5

you can try adding this line:

zip.CompressionLevel = Ionic.Zlib.CompressionLevel.BestCompression; 

but as @JonSkeet said it's not going to do much. Pictures and videos don't compress more thatn they already are.

It didn't work for OP but for future reference: enter image description here

Claudius
  • 1,883
  • 2
  • 21
  • 34
  • This might sound funny: With that line of code, the size of the result increased – Khalil Khalaf Apr 18 '16 at 13:53
  • 1
    zip is not meant for pictures. if you want to save space you may convert pictures to different format. Problem with that approach is that it won't work with png Refer here http://stackoverflow.com/questions/4418454/c-seeking-png-compression-algorithm-library – Claudius Apr 18 '16 at 13:57
2

I found a number of examples available on codeplex here: https://dotnetzip.codeplex.com/wikipage?title=CS-Examples&referringTitle=Examples

But the most relevant one I found is this, which features selecting the best compression method:

using (ZipFile zip = new ZipFile())
{
  zip.CompressionLevel= Ionic.Zlib.CompressionLevel.BestCompression;
  zip.AddFile("Datafeed-2008-12-20.csv");
  zip.Save(ZipFileToCreate);
}

I hope this helps.

Adam Nierzad
  • 942
  • 6
  • 19