2013-03-13 2 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>(); 
    } 
} 

사용자가 세션에서 사진 URL을 가지고 있고 버튼을 클릭하여이 기능을 호출하면 파일이 압축되고 사용자 브라우저에서 다운로드가 시작됩니다.

하지만 압축 된 파일을 열려고하면이 오류 얻을 :

Windows cannot open the folder.

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

나는이 오류의 원인이 뭔가 잘못하고 오전?

+0

다운로드 한 zip 파일의 디스크 크기는 얼마입니까? – cfeduke

답변

2

Response.FlushZipEntry.CleanName의 위치를 ​​확인하고 this example에 비슷한 것을 쓰는 것이 문제를 해결하는지 확인하십시오. 또한 @cfeduke에서 example

0

대신의 경우 ASP Response.ContentType = "application/octet-stream을"를 변경 제안하는 'IIS에서 브라우저 다운로드 첨부 파일로 우편을 만들기의 대답은 의 코멘트가' "신청서/우편 번호"

// 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 특유의 것이 아니 었습니다 (저는 크롬을 사용합니다).