Click here to Skip to main content
13,095,658 members (52,604 online)
Rate this:
Please Sign up or sign in to vote.
See more:

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:
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)) {
   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);

Note that if I debug the application, the

is what is taking the time. So closing the reader stream is what takes ~5 seconds.
Posted 25-May-12 0:40am
Updated 25-May-12 2:30am
losmac 25-May-12 7:49am
Maybe it happens, because of constant value for buffer in FileStream.Read, FileStream.CopyTo methods...
Mika 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
Mika 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

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Hi Mika,

Hope this may help you:

Creating Zip Files Easily in .NET 4.5[^]

Sunny K
Mika 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.
Sunnykumar08 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 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.
Mika 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)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web01 | 2.8.170813.1 | Last Updated 25 May 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100