简体   繁体   English

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

[英]Error when creating Zip file using DotNetZip: Unexpected end of archive

I have a method that zips up one or more files using DotNetZip, and it has been working correctly. 我有一种使用DotNetZip压缩一个或多个文件的方法,它已经正常工作。 I received an error for the first time today and it appears to be related to the total size of the archive. 我今天第一次收到错误,它似乎与档案的总大小有关。 Using the same 60mb .tiff image, I would add a few extra copies, test, repeat. 使用相同的60mb .tiff图像,我将添加一些额外的副本,测试并重复。 It worked fine until about 10 images were added, then, when I opened the Zip file using WinRar, I would get the "Unexpected end of archive" error. 在添加大约10张图像之前,它运行良好,然后,当我使用WinRar打开Zip文件时,出现“意外的存档结束”错误。 Testing in this manner, I believe I've ruled out the problem being related to my code or the files (being corrupt or something). 以这种方式进行测试,我相信我已经排除了与我的代码或文件(损坏或某些东西)有关的问题。 The code does not error, only WinRar. 该代码没有错误,只有WinRar。 When I open the Zip file, there is only one file displayed with a size of "0." 当我打开Zip文件时,仅显示一个大小为“ 0”的文件。 So it seems like some sort of size limit is being reached and it's not allowing the archive to be created. 因此,似乎已达到某种大小限制,并且不允许创建存档。 I just don't know which limit it is. 我只是不知道它是哪个限制。 Here is my code, if it helps: 这是我的代码,如果有帮助的话:

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);
}

The accepted answer here did not solve the problem for me, however, employing some of the comments led me down the right path. 此处接受的答案并不能解决我的问题,但是,使用一些评论使我走上了正确的道路。 What it took to solve this for me was simply adding 为我解决此问题只需添加

HttpContext.Current.Response.End();

after all processing was complete (in my case, immediately after the sample block of code, outside of the using block). 在所有处理完成之后(对于我而言,是在代码示例块之后,在using块之外)。

Do you need to Flush the stream after your are done writing to it? 完成写入流后,是否需要刷新流?

HttpContext.Current.Response.Flush();

EDIT: 编辑:

Call HttpContext.Current.ApplicationInstance.CompleteRequest() 调用HttpContext.Current.ApplicationInstance.CompleteRequest()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM