Zipping Files in Asp.net 1.1

I was working on a project where i needed the user to download multiple files so i had the idea of zipping the folder and send it to the user.

Using the free library SharpZipLib ,  i could achieve this purpose.

// Code to Zip a Folder.

string sPath = Server.MapPath("~") + "DownloadFolder";

string sPath = Server.MapPath("~") + "DownloadFolder";
string zipFullPath = Server.MapPath("~") + "DocumentFolder.zip";

ZipOutputStream zipOut = new ZipOutputStream(File.Create(zipFullPath));

foreach(string fName in Directory.GetFiles(sPath))

{

FileInfo fi = new FileInfo(fName);

ZipEntry entry = new ZipEntry(fi.Name);

FileStream sReader = File.OpenRead(fName);

byte[] buff = new byte[Convert.ToInt32(sReader.Length)];

sReader.Read(buff, 0, (int) sReader.Length);

entry.DateTime = fi.LastWriteTime;

entry.Size = sReader.Length;

sReader.Close();

zipOut.PutNextEntry(entry);

zipOut.Write(buff, 0, buff.Length);

}

zipOut.Finish();

zipOut.Close();

Dont forget to import the fully qualified namespace using ICSharpCode.SharpZipLib.Zip;

Best Regards,

Comments

# re: Zipping Files in Asp.net 1.1

Saturday, November 17, 2007 2:00 PM by clarkhook

how to zip a file.

The leading UI suite for ASP.NET - Telerik radControls
Outstanding performance. Full ASP.NET AJAX support. Nearly codeless development.