2014-10-10 6 views
2

"스트림 작업을 추구 지원하지 않습니다"와 함께 실패DotNetZip 다음과 같이 스트림에서 압축 해제 나는 C#에서 DotNetZip를 사용하고

public static void unzipFromStream(Stream stream, string outdir) 
{ //omit try catch block 
    using (ZipFile zip = ZipFile.Read(stream)){ 
     foreach (ZipEntry e in zip){ 
      e.Extract(outdir, ExtractExistingFileAction.OverwriteSilently); 
     } 
    } 
} 

스트림 그러나

WebClient client = new WebClient(); 
Stream fs = client.OpenRead(url); 

를 사용하여 얻을 수있다, 나는 얻었다 다음 예외가 발생했습니다

exception during extracting zip from stream System.NotSupportedException: This stream does not support seek operations. 
at System.Net.ConnectStream.get_Position() 
at Ionic.Zip.ZipFile.Read(Stream zipStream, TextWriter statusMessageWriter, Encoding encoding, EventHandler`1 readProgress) 

서버 측 (ASP.NET MVC 4)에서 FilePathResult 또는 FileStreamResult 둘 다이 예외의 원인입니다.

스트림을 클라이언트 측에서 다르게 가져와야합니까? 또는 서버를 "검색 가능한"스트림으로 반환하는 방법은 무엇입니까? 감사!

답변

5

데이터를 파일 또는 메모리로 다운로드 한 다음 검색을 지원하는 MemoryStream 또는 다른 스트림 유형을 만들어야합니다. 예를 들면 :

WebClient client = new WebClient(); 
client.DownloadFile(url, filename); 
using (var fs = File.OpenRead(filename)) 
{ 
    unzipFromStream(fs, outdir); 
} 
File.Delete(filename); 

또는 데이터가 메모리에 맞는 경우 :

byte[] data = client.DownloadData(url); 
using (var fs = new MemoryStream(data)) 
{ 
    unzipFromStream(fs, outdir); 
} 
+0

덕분에, 짐! 데이터 크기는 메모리에 의해서만 제한됩니까? 또는 C#에서 contrains에 의해? – totoro

+1

@green'MemoryStream'에는 크기 제한이 있습니다 (최대 값에서는'Int32.MaxValue' 바이트이지만 OutOfMemoryExceptions에서는 실패 할 것입니다). 'FileStream'은 하드 드라이브의 크기에 의해서만 제한됩니다. –