2

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?

JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
Ramesh.kbvr
  • 763
  • 6
  • 15
  • 31

2 Answers2

2

I'd take a guess, you should call:

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

Just before you invoke df.GetBuffer()

Ron Klein
  • 9,178
  • 9
  • 55
  • 88
  • +1 as I have run into issues with SharpZipLib before that stemmed from me not flushing things when they needed to be flushed. – Shibumi Mar 28 '11 at 22:33
  • df is a MemoryStream and shouldn't need a flush. MemoryStream overrides Flush() with an empty method. – GoetzOnline Mar 19 '15 at 04:21
0

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

Felice Pollano
  • 32,832
  • 9
  • 75
  • 115