2011-03-28 114 views
2

我正在使用ICSharpCode.SharpZipLib.Zip压缩文件和文件夹,并使用response.Binary写入将它作为内存流传递。ICSharpCode.SharpZipLib.Zip crc32

这里是我的代码:

MemoryStream df= new MemoryStream();     
ZipOutputStream s = new ZipOutputStream(df); 
s.SetLevel(9); 

byte[] data = (byte[])file.OpenBinary(); 
s.Write(data, 0, data.Length); 
s.Finish(); 

s.Close(); 
byte[] outBuf = df.GetBuffer();   
Response.Expires = 0;     
Response.Buffer = true;     
Response.ClearContent();     
Response.AddHeader("content-disposition", "inline; filename="out.zip"); 
Response.ContentType = "application/zip"; 
Response.BinaryWrite(outBuf); 
HttpContext.Current.ApplicationInstance.CompleteRequest(); 

当我尝试打开out.zip文件,它是说,zip文件已损坏或 损坏,并且CRC值显示为000000。

这是什么解决方案? 为什么会发生此错误?

回答

2

我会采取一种猜测,你应该叫:

s.Flush(); 
df.Flush(); 

调用df.GetBuffer()

+0

+1,因为我遇到SharpZipLib问题之前,由于我没有冲洗需要刷新的东西。 – Shibumi 2011-03-28 22:33:32

+0

它不工作。任何其他解决方案。? – 2011-03-29 13:20:09

+0

df是一个MemoryStream,不应该需要刷新。 MemoryStream用空方法重写Flush()。 – GoetzOnline 2015-03-19 04:21:44

0

尝试关闭之前显式刷新流“S”之前。