[英]Error when creating Zip file using DotNetZip: Unexpected end of archive
我有一種使用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);
}
此處接受的答案並不能解決我的問題,但是,使用一些評論使我走上了正確的道路。 為我解決此問題只需添加
HttpContext.Current.Response.End();
在所有處理完成之后(對於我而言,是在代碼示例塊之后,在using塊之外)。
完成寫入流后,是否需要刷新流?
HttpContext.Current.Response.Flush();
編輯:
調用HttpContext.Current.ApplicationInstance.CompleteRequest()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.