2011-03-28 85 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」之前。