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:
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
fileStream.Close();
is what is taking the time. So closing the reader stream is what takes ~5 seconds.