2014-10-10 61 views
2

我在C#中使用DotNetZip从流解压缩如下:使用DotNetZip失败,“流不支持查找操作”

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

但是获得

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); 
     } 
    } 
} 

流,我以下例外

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),返回FilePathResultFileStreamResult都导致此异常。

我应该在客户端获得不同的流吗?或者如何让服务器返回一个“可搜索”流?谢谢!

回答

5

您必须将数据下载到文件或内存中,然后创建FileStreamMemoryStream或其他支持查找的流类型。例如:

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 2014-10-10 05:13:50

+1

@green'MemoryStream'有一些大小限制(最大为“Int32.MaxValue”字节,但在使用OutOfMemoryExceptions之前可能会失败)。 'FileStream'只受硬盘大小的限制。 – 2014-10-10 07:46:56

相关问题