This project is read-only.

file sizes after adding files (solved)

Mar 13, 2010 at 1:55 PM
Edited Mar 13, 2010 at 1:55 PM


how to get file size after adding file to zip, before saving zip?

using (ZipFile zip = new ZipFile())


 	 zip.AddProgress += new EventHandler<AddProgressEventArgs>(zip_AddProgress);




void zip_AddProgress(object sender, AddProgressEventArgs e)
       totalsize += ????;
Best regards, Sergey





Mar 13, 2010 at 6:48 PM

I don't know what you mean by "total size".

For each entry, there are 2 size quantities, compressed and uncompressed size.  These quantities are known as the entries are zipped, not as they are added. In DotNetZip, "add" means the files are added to the list of files to be zipped, upon Save().  There's little information collected at the time of Add. 

In your own AddProgress evvent handler, you could instantiate a FileInfo() for each file being added, and compute the aggregate uncompressed size of all entries being added.  This works only if all entries are filesystem files. It's possible that some entries are streams, in which case you won't know the size of the uncompressed entry until you read the stream all the way through.

Within a SaveProgress event handler, your application can get the total bytes to read (on-disk size of the file) and the total bytes read so far.  After each entry has been completely compressed, the SaveProgress event fires again, and at that point you can get the compressed size of the entry. 

There's no way to know the total size of the zip file until after compression completes.

Check the documentation for the SaveProgress event for an illustration of how to do it.


Mar 13, 2010 at 8:23 PM

Cheeso, thanks.

I tried to make 2 progress bars: first - file progress, second - total progress. For total progress bar i want to set maximum value - total size of all files, than in SaveProgress event i update total progress bar. FileInfo class give me file length, but i need "size on disk" to update progress bar correct. I find how to calculate it next way: "diskSize = 4096 * ((fileInfo.Length + 4096 - 1)/ 4096)" where 4096 - is clusterSize for ntfs file system.

Thanks for help, i will use AddProgress event and FileInfo class.

Mar 15, 2010 at 11:09 AM

Ah, yes - ok, what you describe makes sense.

In SaveProgressEventArgs, there is a TotalBytesToTransfer property.  It refers to only the current entry, though.  What you're doing is counting the bytes for all entries.

I'm not sure why you are fiddling with the cluster size. That ought to be a detail you can ignore.  FileInfo.Length should give you what you want, without elaboration.