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,