Ramblings of a mad man

The scattered thoughts of an IT professional living in South Africa.

Implementing SharpZip on the web – Zipping multiple files to an archive

with 2 comments

I was tasked to provide a solution whereby the IIS server sends a zipped file to the client containing the files they had selected. After toying around with the .net 2.0 GZipStream I went about investigating the popular SharpZip library. Although mainly used for client side zipping operations where the local machine the application is run on has the correct security rights to run files etc, I decided to put it to the test and implement it on the web in an ASP.net (C#) solution.

The result was fantastic. Here is pseudocode for a possible web implementation. The webserver writes files to a memory stream, when completed and the RetrieveZIP() function is called, the byte[] is passed through to the server side page and streamed back to the client via BinaryWrite().

Pseudo Class:-

Class myZipClass
{

void CreateZIP()

myStream = new MemoryStream();
archive = ZipFile.Create(myStream);
archive.BeginUpdate(new MemoryArchiveStorage());

void AddFilesToZIP(byte[] filestream, string filenameID)

archive.IsStreamOwner = true;
m = new MemoryDataSource(filestream); //File stream is a converted Byte[] to IStaticDataSource
archive.Add(m, filenameID);

byte[] RetreiveZIP()

archive.CommitUpdate();
myStream.Close();
myStream.Flush();
return myStream.ToArray();

}

On the ASP side:-

byte[] finalByteArray = “byte[] returned by RetrieveZIP()”
Response.ContentType = “application/zip”;
Response.AddHeader(“Content-Disposition”, “attachment; filename=Download.zip”);
Response.BinaryWrite(finalByteArray);
Response.Flush();

Till next time, take care.

Written by microdotsagamedev

October 12, 2007 at 9:10 am

2 Responses

Subscribe to comments with RSS.

  1. [...] You can read the rest of this blog post by going to the original source, here [...]

  2. Thanks for the hint mate, bt it doesnt work for me =/

    Fabricio

    May 13, 2009 at 10:48 pm


Leave a Reply