tags:

views:

1980

answers:

6

Hi,

I am trying to programatically unzip a zipped file.

I hace tried using the System.IO.Compression.GZipStream class in .NET, but when my app runs (actually an unit test) I get this expection:

System.IO.InvalidDataException: The magic number in GZip header is not correct. Make sure you are passing in a GZip stream..

I now realize that a .zip file is not the same as a .gz file, and that GZip is not the same as Zip.

However, since I'm able to extract the file by manually double clicking the zipped file and then clicking the "Extract all files"-button, I think there should be a way of doing that in code also.

Therefore I've tried to use Process.Start() with the path to the zipped file as input. This causes my app to open a Window showing the contents in the zipped file. That's all fine, but the app will be installed on a server with noone around to click the "Extract all files"-button.

So, how do I get my app to extract the files in the zipped files?

Or is there another way to do it? I prefer doing it in code, without downloading any third party libraries or apps; the security department ain't too fancy about that...

+3  A: 

Use the DotNetZip library at http://www.codeplex.com/DotNetZip

Boo
Hmmm... But that's a third party library!
Petteri
How very observant of you. Unless you feel like spending several months implemening your own Zip file reader, its your best option.
Boo
+1  A: 

Standard zip files normally use the deflate algorithm.

To extract files without using third party libraries use DeflateStream. You'll need a bit more information about the zip file archive format as Microsoft only provides the compression algorithm.

You may also try using zipfldr.dll. It is Microsoft's compression library (compressed folders from the Send to menu). It appears to be a com library but it's undocumented. You may be able to get it working for you through experimentation.

codeelegance
I'm trying out the DeflateStream class. This time I get System.IO.InvalidDataException: Block length does not match with its complement..
Petteri
As I said above, Microsoft only provided the algorithm. You'll need info on the zip archive format as well. http://en.wikipedia.org/wiki/ZIP_(file_format) should get you started. See the references at the bottom of the page for links to more detailed info.
codeelegance
I also stumbled acrossed System.IO.Packaging.Package in .NET 3.5. It looks like it may do the trick though its not very intuitive.
codeelegance
A: 

From here :

Compressed GZipStream objects written to a file with an extension of .gz can be decompressed using many common compression tools; however, this class does not inherently provide functionality for adding files to or extracting files from .zip archives.

RedWolves
A: 

You haven't specified which version of .Net you are using, but there's a (dirty) way to do this: Visual J# includes a MS implementation of java.util.zip...

See this article for details on that

Tetsujin no Oni
I'm using .NET 3.5. I saw that article too, the future will tell if I will use Java. Thanks.
Petteri
Very dirty. The J# runtime is HUUUUGE, no longer supported, and the zip portion of it is old and buggy. Avoid this.
Cheeso
I agree on the dirtiness, but if corporate policy favors Microsoft dependencies and disallows open source...
Tetsujin no Oni
If that's the case, the new packaging classes are preferred! Whatever the Corp Policy, it isn't wise to depend on unsupported, buggy code.
Cheeso
+3  A: 

We have used SharpZipLib successfully on many projects. I know it's a third party tool, but source code is included and could provide some insight if you chose to reinvent the wheel here.

Chris Conway
I tried using SharpZipLib and it worked fine. I guess I'll have to see if the prohibition against third party libs and apss is a strict rule or more of a guidline.
Petteri
I don't know about your company, but my experience has always been that it's possible to get an exception to that sort of rule if you write up a *business case* description of why you want the exception. Point out the cost savings v. DIY, as well as the fact that the source can be examined. As a fallback, you can often get permission to use the source even if they won't let you use the dll--then just compile it yourself (or at least the parts you actually need to use...).
RolandTumble
A: 

I found sample code to unzip files in .net c#