Unzip files programmatically in C# - #ZipLib - Thea Burger's Blog

Thea Burger's Blog

Wouldn't you like to know...

News

Photo's!!!

About me

I'm Reading: General Blogs

I'm Reading: Technical Blogs

Unzip files programmatically in C# - #ZipLib

So this was the first time I had to do this and I started to google... I was looking for a plain and simple way of unzipping a previously zipped file.

The results included:
  • System.IO.Compression that has been added in .NET 2.0 - used to compress and decompress streams. This didn't exactly look like what I wanted and a lot of work :P
  • WinRAR add-on that contained an UnRAR.dll for Windows. Michael A. McCloskey created a wrapper class for this which makes it much easier to use, but unfortunately I ran into troubles with .NET 2.0.
  • WinZip has a command line add-on which allows you to use batch files to extract the files from the zip, but a license for WinZip 10 Pro is needed.
  • #ziplib/SharpZipLib which was created by the guys from SharpDevelop. Entirely C#. This definitely looked like what I was looking for....
So I downloaded #ziplib and started looking at examples and jumped in and wrote some code... Created some GZipOutputStreams and FileStreams and tested and it didn't work and I played around a bit more... During my searches I came across a post where someone mentioned FastZip, which is part of #ziplib and can be used for basic functions....

After a days searching and playing around and lines and lines of unnecessary code all I needed was the following:

using ICSharpCode.SharpZipLib.Zip;

FastZip fz = new FastZip();       
fz.ExtractZip(zipFile, targetDirectory,"");


Can you freaking believe it!!?

Posted: Jan 17 2006, 07:24 PM by Thea Burger | with 22 comment(s)
Filed under:

Comments

Ernst Kuschke said:

Now if only I could find something like this for .cab files...
# January 18, 2006 12:21 AM

derek said:

Nice find. I'll definitely be using this in future!
# January 18, 2006 9:29 AM

Daniel said:

sweet! i've become quite familiar with the commandline interface of WinRAR. but calling it programmatically isn't so nice.
# January 18, 2006 11:03 AM

rudolf said:

I've been using it for a number of years and it works great!
# January 19, 2006 7:41 AM

Siva said:

Hey thanks a lot man. it works great !!
# January 19, 2006 9:21 PM

Prabhakar said:

Hi.. Nice One .. But How I can get file names of extracted file..one by one...

Like If I have 1.jpg 2.jpg 3.jpg in img.zip file. Now when I will extract the file it will be saved in directroy.

How I will get the file names?

Thanks
# January 27, 2006 7:17 AM

johnccr said:

ZipFile zip = new ZipFile(ZipPath);
//List compressed files
foreach(ZipEntry compfile in zip) {
if (compfile.IsFile) {
//call compfile.Name to ge the file name Notice it includes relative path
}
}
# February 1, 2006 7:35 PM

Pieter said:

Sweet, spaar my baie tyd vandag! :D
# February 2, 2006 12:16 AM

Sam said:

*scratches head* I do kind of remember that zip-support already is in the .net framework. Only thing is, it is in the language framework of j#, but you can use it from any other .net language, too.
You only have to use the right libs. You don't need to install anything.

More information about this in comparison to other solutions:
http://msdn.microsoft.com/msdnmag/issues/03/06/ZipCompression/
# February 24, 2006 9:04 PM

Martin said:

I was running into this problem without knowning that it might be one!
Sam's way seems to be the best. The Java (even way dated as 1.1.4)
zip support is good.

So, why is it that there are commercial lib's out there for some 100$!
Only argument seems to be its C# and not J#. But then, I would also
never use J# - but making use of a part of .NET that happens to support
J# seems to be fine with me.
# March 12, 2006 6:15 PM

DMINATOR said:

Yes it is definetly the best way to unzip files right now. But be carefull unzipping empty files, I hope they fix the bug soon :)
# March 16, 2006 1:34 PM

Dave said:

can't unzip file. I have a zip file which zip by pkzip.exe and try to unzip the file using fastzip function. I got an error: Unknow Compression Method 6. Can some explain to me why?????? If I tried to open my zip file from winzip and zip my file back using winzip then I  don't get any errors...
# March 30, 2006 3:27 AM

Carlos said:

I new this was too good to be true.  This does not work for me. I have tried everything possible, but still get error "Wrong Local header signature: 0x4E4D4357".  What am I doing wrong?
# April 18, 2006 1:29 AM

Chitra said:

How to find the number of files in a .zip file? I would like to use a progressbar control and refresh the progressbar as it unzips the files to the target dir.
I would appreciate any inputs
Thanks
# May 12, 2006 12:54 AM

Mohan said:

We are trying to extract files from cab files. Can u please help me, how to proceed further.
# May 23, 2006 11:17 AM

Steve said:

Is there a way to unzip password protected zip files? i checked out the SharpZipLib documentation but couldn't find anything. Thanks!
# June 2, 2006 11:47 PM

Pravin Sharma said:

Thank you very much...
# August 18, 2006 3:15 PM

Anne said:

If I want to unzip only some files that I need in the zip files, how do I do that? for examples, in my.zip (file1,file2,file3,file4). I only want to extract file1 and file3. How do I do that? Anne
# September 5, 2006 5:21 PM

Sireesh said:

Exactly what i am looking for. Thanks a lot sir.
# September 6, 2006 11:54 PM

ben said:

Test result: I tried it, but it didn't work. most zip files extract nothing, some zip files extract one or two files. it's just funny. is that because of the dll version is too old? 1,I downloaded the "#ziplib/SharpZipLib " http://www.icsharpcode.net/OpenSource/SharpZipLib/ 2, I tried both vs2003, and vs2005 with C#
# September 7, 2006 4:19 AM

Unzip C# Library | Rusty's Technology Blog said:

Pingback from  Unzip C# Library | Rusty's Technology Blog

# June 7, 2007 11:18 PM

ASpeak » SharpZipLib Decompression said:

Pingback from  ASpeak » SharpZipLib Decompression

# August 12, 2008 8:19 PM