创建一个ZIP包会减慢速度 [英] Creating a ZIP package slows down
问题描述
我正在努力解决一个小问题,希望有人能对此有所启发.
问题是,当我使用ZipPackage
-class创建Zip文件时,程序包本身已成功创建,但是在某些情况下,程序包的创建速度大大降低.
我可以使用相同的文件不断地重复该问题,但是我找不到原因.出现问题的文件集包含大量二进制文件,这些二进制文件已添加到zip,然后在末尾添加了一些文本文件.添加100个以上的二进制文件很顺利,但是最后添加文本文件时,创建速度变慢,因此将5 kB文本文件添加到程序包大约需要5秒钟.
到目前为止的观察结果:
- 文件数量似乎没有受到影响.如果首先添加了几个二进制文件,最后添加了文本文件,则会出现问题
- ,如果我颠倒了将文件添加到程序包中的顺序,则程序包的创建速度很快(文本文件首先,然后是二进制文件)
- 每个二进制文件为1-2 MB,大约添加了150个二进制文件.
- 仅添加了数百个二进制文件(最后没有文本文件)包装中的产品)表现出色
- 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
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:
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();
是在花时间.因此,关闭阅读器流大约需要5秒钟.
is what is taking the time. So closing the reader stream is what takes ~5 seconds.
推荐答案
Mika,
希望这对您有所帮助:
在.NET 4.5中轻松创建Zip文件 [ ^ ]
谢谢,
Sunny K
Hi Mika,
Hope this may help you:
Creating Zip Files Easily in .NET 4.5[^]
Thanks,
Sunny K
这篇关于创建一个ZIP包会减慢速度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!