2013-03-13 65 views
2
壓縮文件時

我有這個功能,我使用壓縮從用戶的會話文件的列表,然後將其分流到用戶的瀏覽器下載:「無效文件」錯誤與SharpZipLib

public static void DownloadAllPhotos() 
{ 
    HttpContext.Current.Response.AddHeader(
     "Content-Disposition", "attachment; filename=Photos.zip"); 
    HttpContext.Current.Response.ContentType = "application/zip"; 

    List<string> photos= new List<string>(); 

    if (HttpContext.Current.Session != null && 
     HttpContext.Current.Session["userPhotos"] != null) 
    { 
     photos = (List<string>)HttpContext.Current.Session["userPhotos"]; 
    } 

    using (var zipStream = new 
     ZipOutputStream(HttpContext.Current.Response.OutputStream)) 
    { 
     foreach (string photoUrl in photos) 
     { 
      byte[] fileBytes = File.ReadAllBytes(photoUrl); 

      var fileEntry = new ZipEntry(
       Path.GetFileName(photoUrl)) 
      { 
       Size = fileBytes.Length 
      }; 

      zipStream.PutNextEntry(fileEntry); 
      zipStream.Write(fileBytes, 0, fileBytes.Length); 
     } 

     zipStream.Flush(); 
     zipStream.Close(); 

     // reset session 
     HttpContext.Current.Session["userPhotos"] = new List<string>(); 
    } 
} 

當用戶在他們的會話中有照片網址時,他們點擊一個按鈕來調用這個功能,這些文件被壓縮並且下載在用戶的瀏覽器中開始。

但是當我嘗試打開該壓縮文件,我得到這個錯誤:

Windows cannot open the folder.

The compressed folder "{Path to my file}" is invalid.

我是不是做錯了什麼是造成這個錯誤?

+0

下載的zip文件的磁盤大小是多少? – cfeduke 2013-03-13 03:30:24

回答

2

檢查Response.FlushZipEntry.CleanNamethis example中的位置,看看是否寫了類似的東西來糾正問題。

0

在每對@cfeduke另外example的回答,則在評論‘創建一個Zip爲IIS’那個建議改變Response.ContentType =‘應用/八位字節流’,而不是一個瀏覽器下載附件「申請/郵編」

// If the browser is receiving a mangled zipfile, IIS Compression may cause this problem. Some members have found that //Response.ContentType = "application/octet-stream" has solved this. May be specific to Internet Explorer.

爲我工作。它不是IE特定的(我使用Chrome)。

相關問題