2013-03-13 37 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)。