Checking ZipFile size

Oct 15, 2009 at 6:36 PM


After I do something like this:

ZipFile zipFile = new ZipFile();


Is there a way to check the size of the archive?



Oct 15, 2009 at 8:20 PM

After you save it, you can know the size of the archive. Not before.


Oct 15, 2009 at 8:30 PM

Thank you for replying!

It would have been convenient to know about the size before, this way it would be possible to determine whether to save to FileStream or MemoryStream. For example, if the size is less than 400K I would save it to a MemoryStream instead. I assume it is just hard to know before the file is compressed?


Oct 15, 2009 at 9:26 PM

It's impossible to know, until the compression is performed.

One approach would be to compress twice.  The first time, you'd compress to a stream that does not have a backing store, but counts the bytes written.  For example, a wrapper around Stream.Null that computes the number of bytes that is written.  With that size, you could decide whether to use a file or a memory stream, and then compress again. 

In my opinion that's a lot of logic to perform this optimization.  Why not just use the filesystem and be done with it?  Why go to all the trouble of a bi-modal save approach?  What are you trying to save?  

Oct 15, 2009 at 9:53 PM

Altimately, I need to pass the data as a byte array to be stored somewhere. It could be in many cases that the file size is not large and saving into MemoryStream without temporarily persisting would be easier. As you said, since it is impossible, it is OK to just save it to the file first, and then, if the size is large, I will be sending data in fragments.