ICSharpCode.SharpZipLib.Zip crc32

2019-07-20 15:04发布

问题:

I am using ICSharpCode.SharpZipLib.Zip to compress the files and folders and pass it as memorystream using response.Binary write.

Here is my code:

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

When I try to open the out.zip file, it is saying that the zip file is either corrupted or damaged, and the crc value is showing as 000000.

What is the solution for this? Why is this error is occurring?

回答1:

I'd take a guess, you should call:

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

Just before you invoke df.GetBuffer()



回答2:

Try to explicitly flush the stream "s" before close.