Damaged zip archive

Jun 1, 2010 at 9:44 AM
Edited Jun 1, 2010 at 3:38 PM


In almost all cases library works perfectly. But in some cases I am getting damaged archive and strange "$" entry inside. 

I am getting this especially when file (I am zipping xml files) contains attribute with a lot of byte data.

When I am trying to open archive I get: "ArchivedResults.zip: Unexpected end of archive"

Is there anybody who faced such problem?



Jun 3, 2010 at 9:41 AM

If xml doesn't contains base64 data then it works. Since WriteXml method encode byte array to base64 string I get error in zip archive. 

Jun 3, 2010 at 11:42 AM

Can you show me some code that produces the error. A simple test case.

How do you add entries to the archive?

Jun 3, 2010 at 12:00 PM
Edited Jun 3, 2010 at 1:47 PM

Thanks for answering.

MemoryStream ms = new MemoryStream();
			using (ZipFile zip = new ZipFile())
				using (Stream s = GetStream(_transformatedFileName, FileMode.Open))
					zip.AddEntry("Results.xml", s);
					zip.CompressionLevel = CompressionLevel.BestCompression;




Then send ms object over WCF inside resultMessage (OutputStream contains archived stream)
After I receive message I am writing it to the hard disk:


using(Stream s = GetStream(filepath, FileMode.Create))
						//Read byte array from OutputStream
						byte[] byteArray = new byte[resultMessage.OutputStream.Length];
						int count = resultMessage.OutputStream.Read(byteArray, 0, (int)resultMessage.OutputStream.Length);

						//Write to isolated storage
						s.Write(byteArray,0, byteArray.Length);


An hour ago I noticed that problem is in sending it over WCF. If instead it I just save it to hard disk then it works.

Jun 3, 2010 at 2:09 PM

Ahh, Well then it seems like it is not a problem in DotNetZip.

The most common problem when saving to a MemoryStream is that people forget to call .Seek() on the memory stream, after writing it, and before reading it. There were 2 people this week that had this problem.  Maybe that's your problem here. If I'm right, you'd need to call ms.Seek(0, SeekOrigin.Begin) before transmitting ms over the WCF response.

Good luck!

Jun 3, 2010 at 2:28 PM

Probably you are right. maybe somewhere I didn't set position of stream to 0. I have a very big flow of data. So it is hard to find where is the problem. But after your questions

I analyze all flow and understand that something wrong is with stream or WCF. Anyway thanks on quick answering.