Click here to Skip to main content
15,867,141 members
Please Sign up or sign in to vote.
5.00/5 (3 votes)
See more:
Hi,

I'm struggling with a small problem and I hope that someone could shed some light to it.

The problem is that when I create a Zip-file using ZipPackage-class, the package itself is created succesfully, but in some cases the creation of the package slows down dramatically.

I can repeat the problem constantly with the same files, but I haven't been able find a reason for this. The problem occurs with a file set containing larger number of binary files added to the zip and then adding a few text files in the end. Adding 100+ binary files goes smoothly but in the end when the text files are added, the creation slows down so that adding a 5 kB text file to the package takes roughly 5 seconds.

Observations so far:

  • the amount of files doesn't seem to affect. If several binary files are added first and the text files in the end, the problem occurs
  • if I reverse the order in which I add the files to the package, the creation of the package is fast (text files first, then binary files)
  • each binary file is 1-2 MB and there is roughly 150 binary files added.
  • just adding several hundred binary files (without text files in the end of the package) is performing excellent


Has anyone else been experiencing same kind of problems and/or do you have ideas why this is happening and how to prevent it?

The code I'm using to add a single file to the Zip is:
C#
System.Uri uri;
System.IO.Packaging.PackagePart part;

using (System.IO.Packaging.Package zip = System.IO.Packaging.ZipPackage.Open(zipFilename, System.IO.FileMode.OpenOrCreate)) {
   uri = System.IO.Packaging.PackUriHelper.CreatePartUri(new System.Uri(".\\" + new System.IO.FileInfo(fileToAdd).Name, System.UriKind.Relative));
   if (zip.PartExists(uri)) {
      zip.DeletePart(uri);
   }
   part = zip.CreatePart(uri, "", System.IO.Packaging.CompressionOption.Normal);
   using (System.IO.FileStream fileStream = new System.IO.FileStream(fileToAdd, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Read, 8192)) {
      using (System.IO.Stream dest = part.GetStream()) {
         fileStream.CopyTo(dest, 8192);
         dest.Close();
      }
      fileStream.Close();
   }

Note that if I debug the application, the
C#
fileStream.Close();

is what is taking the time. So closing the reader stream is what takes ~5 seconds.
Posted
Updated 25-May-12 2:30am
v2
Comments
Maciej Los 25-May-12 7:49am    
Maybe it happens, because of constant value for buffer in FileStream.Read, FileStream.CopyTo methods...
Wendelius 25-May-12 10:40am    
Thanks losmac. I've tried different buffer sizes from 2 to 64 kB but the behaviour is always the same. With the text files, it slows down in the end...
Nelek 25-May-12 16:06pm    
Interesting question. My 5. Sorry I can not help
Wendelius 27-May-12 8:42am    
Thanks, been struggling with this for some time and it isn't interesting to me anymore, just annoying :D

1 solution

Hi Mika,

Hope this may help you:

Creating Zip Files Easily in .NET 4.5[^]



Thanks,
Sunny K
 
Share this answer
 
Comments
Wendelius 25-May-12 7:21am    
Thanks for the answer. Yes it's a good article, but the problem I'm facing is an unexplained slow down when adding files to an archive.

The creation of the Zip package is succesfull so the application works as expected, but the performance in certain circumstances is very poor.
Sunny_Kumar_ 25-May-12 7:56am    
Hi Mika,

I gave your code a real execution on my IDE. I'm using Visual Studio 2010. It's really working fast as expected...no slow down is there. I've tested this code zipping an 11MB file 10 times with different names (I ran a loop for 10 times) and it gave me the output zip file in less than 6 seconds. Hence I suspect there must be something happening in the middle.
Wendelius 25-May-12 8:28am    
Thanks for the feedback! Yes I'm having same kind of (good) performance with smaller amounts of data. Is it possible to do a test run which would be close to my situation:
- add 200 binary files, for example pictures to the package
- then add few text files into the same package

The total size of the zip causing the problems for me is ~300 MB. Based on the experiments I've done, the slow down doesn't occur with smaller amounts of binary files but somewhere after 100 binary files in the beginning and specifically adding text files after them, I experience a very poor performance.

One funny thing is that if I continue to add binary files into the zip (let's say 500 files), the performance is good. The slow down happens only when I add the few text files in the end.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900