Geeks With Blogs
AzamSharp Some day I will know everything. I hope that day never comes.

Sometimes in an application you need to unzip the folder and read the contents out of that folder. For unzipping purpose you can use the FREE SharpZipLib Library. Check out the code below which is used to unzip the zip folder and place the unzipped folder in the Files directory of the application.

First, the Button_Upload is used to send the zip file to the Server's Folder.

protected void Btn_Upload_Click(object sender, EventArgs e)

{

string folder = Server.MapPath("~/Files/");

string filePath = FileUpload1.PostedFile.FileName;

string fileName = System.IO.Path.GetFileName(filePath);

FileUpload1.PostedFile.SaveAs(folder + fileName);

// Unzip the file

UnzipFile(filePath, folder);

}

After the folder has been transfered to the folder you can use the following code to unzip it.

private void UnzipFile(string zipFilePath, string folder)

{

ZipInputStream s = new ZipInputStream(File.OpenRead(zipFilePath));

ZipEntry theEntry;

while ((theEntry = s.GetNextEntry()) != null)

{

string directoryName = Path.GetDirectoryName(theEntry.Name);

string fileName = Path.GetFileName(theEntry.Name);

string serverFolder = Server.MapPath("~/Files/");

// create directory

Directory.CreateDirectory(serverFolder +directoryName);

if (fileName != String.Empty)

{

FileStream streamWriter = File.Create(( serverFolder + theEntry.Name));

int size = 2048;

byte[] data = new byte[2048];

while (true)

{

size = s.Read(data, 0, data.Length);

if (size > 0)

{

streamWriter.Write(data, 0, size);

}

else

{

break;

}

}

streamWriter.Close();

}

}

s.Close();

}

 

You can download the SharpZipLib Library from the following link:

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

Posted on Thursday, August 24, 2006 10:00 PM | Back to top


Comments on this post: Unzipping Folders Using SharpZipLib

# re: Unzipping Folders Using SharpZipLib
Requesting Gravatar...
Thank you Mate. Very useful code.
Left by Keen on Mar 30, 2007 10:00 PM

# re: Unzipping Folders Using SharpZipLib
Requesting Gravatar...
Thanx very much....
Solved my problem
Left by Vipul Mehta on Dec 20, 2007 11:42 PM

# re: Unzipping Folders Using SharpZipLib
Requesting Gravatar...
thanks very much it wonderful code
Left by anhtin on Dec 26, 2007 7:32 PM

# re: Unzipping Folders Using SharpZipLib
Requesting Gravatar...
Thanks what was just what I was looking for!
Left by Jay on Jan 30, 2008 7:13 AM

# re: Unzipping Folders Using SharpZipLib
Requesting Gravatar...
Hi
I am trying unzip several zip files using Sharpziplib when I try to unzip the 2nd zipfile its always returning me NullReferenceException. how can I do it?
This is my code that I used to pass the zipPath

DirectoryInfo di = new DirectoryInfo(zipsPath);

FileInfo[] zFiles = di.GetFiles("*.zip");

foreach (FileInfo zFile in zFiles)
{
string zPath = zFile.FullName;
string zName = zFile.Name;
MessageBox.Show(zName, "zip Name");
MessageBox.Show(zPath, "zip Path");
decompress(zName,zPath);
}

For unzipping I used your code.

why I am getting this? Any Help.

Left by aruna on Feb 04, 2008 10:27 PM

# re: Unzipping Folders Using SharpZipLib
Requesting Gravatar...
i am useing this code to zip the file, but i am getting error

Access to the path 'D:\Projects\sandbox\IsharpZipper\IsharpSampleApp\IsharpSampleApp\ZipFolder\ZipTest.sln' is denied.

Any idea :)
Left by Rajeev on May 01, 2008 4:58 PM

Your comment:
 (will show your gravatar)


Copyright © Mohammad Azam | Powered by: GeeksWithBlogs.net