Invalid zip-file

Apr 6, 2010 at 10:48 AM

Hi.

I'm trying to use your code to make a website backup feature for my CMS. All files from the website, should be added to a zip-file that the user can store offline as a backup.

But, the zip-file is not valid for some reason. I'm using the latest version of your code. I have created a test file that you can see here: www.kitecms.com/test.zip

I use this code:

MemoryStream ms = new MemoryStream();
ZipStorer zip = ZipStorer.Create(ms,"");

System.IO.FileInfo fileInfo = new System.IO.FileInfo(Server.MapPath("/web.config"));
FileStream fileStream = File.OpenRead(Server.MapPath("/web.config"));
zip.AddStream(ZipStorer.Compression.Store, "/web.config", fileStream, fileInfo.LastWriteTime, "");
fileStream.Dispose();
streamZip(ms);

HttpContext.Current.Response.Clear();
HttpContext.Current.Response.ContentType = "application/zip";
HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=test.zip");
HttpContext.Current.Response.AppendHeader("Content-Length", ms.Length.ToString());
HttpContext.Current.Response.BinaryWrite(ms.GetBuffer());
ms.Dispose();

HttpContext.Current.Response.End();


I will be very happy, if you can tell me what I'm doing wrong.

TIA
Thomas

Apr 6, 2010 at 2:11 PM

Fixed it myself.

I didn't close the ZipStorer before streaming the object. This meant that the last info in the zip-file was not written and therefore it was not valid.
But, the close-method of the ZipStorer did not work for me, so I split it up calling the WriteEndRecord-part before streaming and the this.ZipFileStream-cleaning after.

Thanks for a nice app :-)

Coordinator
Apr 6, 2010 at 3:09 PM

Hi Thomas,

Zipstorer is derived from IDisposable, so you can use something like:

MemoryStream ms = new MemoryStream();
using (ZipStorer zip = ZipStorer.Create(ms,""))
{
System.IO.FileInfo fileInfo = new System.IO.FileInfo(Server.MapPath("/web.config"));
FileStream fileStream = File.OpenRead(Server.MapPath("/web.config"));
zip.AddStream(ZipStorer.Compression.Store, "/web.config", fileStream, fileInfo.LastWriteTime, "");
fileStream.Dispose();
streamZip(ms);
}

Also you can use ms.Flush() to force stream to complete.

If you find this library useful, please rate it.

Best regards,

Jaime.