使用DotNetZip创建Zip文件时出错:存档意外结束

时间:2012-02-27 17:32:46

标签: c# asp.net zip dotnetzip winrar

我有一个使用DotNetZip压缩一个或多个文件的方法,它已经正常工作。我今天第一次收到错误,它似乎与存档的总大小有关。使用相同的60mb .tiff图像,我会添加一些额外的副本,测试,重复。它工作正常,直到添加了大约10个图像,然后,当我使用WinRar打开Zip文件时,我会得到"意外的归档结束"错误。以这种方式进行测试,我相信我已经排除了与我的代码或文件(腐败或其他东西)相关的问题。代码没有错误,只有WinRar。当我打开Zip文件时,只显示一个大小为" 0的文件。"因此,似乎达到了某种大小限制,并且它不允许创建存档。我只是不知道它的限制。这是我的代码,如果它有帮助:

HttpContext.Current.Response.Clear();
HttpContext.Current.Response.ContentType = "application/zip";
HttpContext.Current.Response.AddHeader("content-disposition", "filename=" + "MyFileName" + DateTime.Now.ToString("MMddyyyy") + ".zip");

using (var zip = new Ionic.Zip.ZipFile())
{
    zip.MaxOutputSegmentSize = 819200; // I tried adding this and it did not fix the problem

    foreach (var file in files)
    {
        zip.AddFile(file.FileLocation, file.ZipFileDirectory).FileName = 
            (!string.IsNullOrEmpty(file.ZipFileDirectory) && (file.ZipFileDirectory != @"\")) ? 
                string.Format(@"{0}\{1}", file.ZipFileDirectory, file.FileName) : 
                file.FileName;
    }

    zip.Save(HttpContext.Current.Response.OutputStream);
}

2 个答案:

答案 0 :(得分:3)

这里接受的答案并没有为我解决问题,然而,采用一些评论使我走上了正确的道路。为我解决这个问题只需添加

HttpContext.Current.Response.End();

所有处理完成后(在我的情况下,紧接在代码块之后,在使用块之外)。

答案 1 :(得分:2)

完成写入后,是否需要刷新流?

HttpContext.Current.Response.Flush();

修改

调用HttpContext.Current.ApplicationInstance.CompleteRequest()