我正在寻找一些方法来加速使用DotNetZip将条目添加到压缩存档中的过程。
当前的问题是,在将所有文件添加到存档之前,响应超时。每个档案中可能有200-500个条目,每个条目的大小在1-4Meg之间。我尝试了一个100个文件的样本,这太慢了。
压缩不是必要的,因为所有文件都是.mp3-文件。
我现在有这样的代码:
private readonly IDictionary<string, Stream> _files = new Dictionary<string, Stream>();
public bool CreateZip(ref MemoryStream result, bool compress = true)
{
using (ZipFile zip = new ZipFile())
{
zip.UseZip64WhenSaving = Zip64Option.AsNecessary;
if (!compress)
zip.CompressionLevel = CompressionLevel.None;
if (_files.Any())
{
foreach (var file in _files)
{
ZipEntry entry = zip.AddEntry(file.Key, file.Value);
entry.Comment = file.Key;
}
}
zip.Save(result);
result.Seek(0, SeekOrigin.Begin);
result.Flush();
}
return true;
}这些文件首先添加到字典中(作为来自RavenFS/RavenFileStorage的流),然后添加到存档中。zip本身也保存到流中。
我考虑过线程处理,但是当向归档文件中添加条目时,库似乎不支持线程处理。
我是否正确地使用了库,还是有任何方法更快?
发布于 2016-08-09 13:34:35
ref参数。它很少需要,在您的CreateZip方法中也没有用。ZipFile直接保存到Response.OutputStream,而不是临时MemoryStream。这将允许您在创建文件时开始对其进行流处理。https://codereview.stackexchange.com/questions/138247
复制相似问题