-->

Error when creating Zip file using DotNetZip: Unex

2019-09-05 05:49发布

问题:

I have a method that zips up one or more files using DotNetZip, and it has been working correctly. 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. 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. 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. When I open the Zip file, there is only one file displayed with a size of "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);
}

回答1:

Do you need to Flush the stream after your are done writing to it?

HttpContext.Current.Response.Flush();

EDIT:

Call HttpContext.Current.ApplicationInstance.CompleteRequest()



回答2:

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).