Tuesday, January 29, 2008

Using SharpZipLib to extract Zip File

I'm trying to extract a zip file from within asp.net, after searching for a while using everyone fav search engine "the you know who" :) , i found this great library call sharpZipLib, and yes it's free
You can download it here :

http://www.icsharpcode.net/OpenSource/SharpZipLib/

to use it just add sharpZipLib.dll reference in your project, and then create a function to extract it.

Here are my function to extract the zip file :

        public ArrayList  ExtractZipFile(string zipFileName, string extractFolder)
{
FileStream inputZipStream = new FileStream(zipFileName,FileMode.Open);
ArrayList fileNameList = new ArrayList();
try
{
ZipInputStream zipStream = new ZipInputStream(inputZipStream);
ZipEntry entryFile = null;
//loop for all the file in zip
while((entryFile = zipStream.GetNextEntry()) != null)
{
FileStream outPutStream = new FileStream(extractFolder + entryFile.Name ,FileMode.Append);
int size = 2048;
byte[] data = new byte[2048];
while (true)
{
size = zipStream.Read(data, 0, data.Length);
if (size > 0)
{
outPutStream.Write(data, 0, size);
}
else
{
break;
}
}
outPutStream.Close();
fileNameList.Add(entryFile.Name);
}
}
catch(Exception ex)
{
throw ex;
}
finally
{
inputZipStream.Close();
}

return fileNameList;
}