How to extract zip file using dotnet framework 4.0 without using third party dlls

32,658

Solution 1

In .NET framework 4.5 now there is System.IO.Compression.ZipFile.ExtractToDirectory(String, String) method.

Solution 2

I'm not sure if it's still available in VS2010, but in earlier versions J# was included in VS and J# includes the java.util.zip class in the vjslib.dll, and since it's part of VS, it's not a third party DLL so you might be able to do it that way.

Here's a CodeProject article showing how to do this: Zip and Unzip from a C# program using J# runtime

Solution 3

The .NET framework itself does not (currently) have official support for ZIP files. There are a couple of high-quality third-party free libraries available, and the J# runtime supports it, as others have noted.

I recommend the third-party solution, but if you absolutely can't do that, then there is one other trick: ZipPackage, which is part of WPF. It sort-of supports ZIP files (augmented with extra metadata), and works well enough for reading most ZIP files.

Solution 4

There is no "good" way to do this. You could obviously implement the ZIP algorithm yourself using publicly available information on the ZIP file structure and classes such as DeflateStream (though even this may not work if it is not compressed using the DEFLATE algorithm). There is some information on possible alternatives in this blog post, but the short version is that you will either have to use a third-party library or re-write one yourself.

If this is a situation where you would be able to release the source code in a GPL'd manner, you could copy-paste the code from a project like SharpZipLib into your own, thereby sidestepping the requirement to use the DLL. Other than that, though, why can't you use third-party DLLs?

Share:
32,658
Apurva Saxena
Author by

Apurva Saxena

If you really want to hear about it, the first thing you'll probably want to know is where I was born, and what my lousy childhood was like, and how my parents were occupied and all before they had me, and all that David Copperfield kind of crap, but I don't feel like going into it, if you want to know the truth. Meet me n analyze urself.......

Updated on August 30, 2020

Comments

  • Apurva Saxena
    Apurva Saxena over 3 years

    I'm in a fix. I need to download a zip file from network location and then decompress it on local machine and use the files. The only constraint is that I cannot use any third party dll.

  • Dan Byström
    Dan Byström almost 14 years
    "...however, this class does not inherently provide functionality for adding files to or extracting files from .zip archives..."
  • Ryan M
    Ryan M almost 14 years
    This does not appear to be a WCF question, and GZipStream isn't going to help with ZIP files, which usually use the DEFLATE algorithm.
  • Thomas Levesque
    Thomas Levesque almost 14 years
    Interesting, I didn't know it was there...
  • Wojtek Turowicz
    Wojtek Turowicz almost 14 years
    You didn't understand my post. I suggested other approach...
  • rafa316
    rafa316 about 11 years
    System.IO.Compression ;)
  • Ivan Ičin
    Ivan Ičin about 11 years
    OOps, quick writing... I'll correct it :)
  • Highmastdon
    Highmastdon about 11 years
    SharpZipLib will fail with files compressed by a unix host system. For example when made on a MacOSX machine, using the internal zip compressor
  • DontFretBrett
    DontFretBrett almost 10 years
    I like how the question specifically asked for a 4.0 solution and a 4.5 answer was selected
  • EKOlog
    EKOlog over 8 years
    And System.IO.Compression.FileSystem for ZipFile class :)
  • Krishnan Venkiteswaran
    Krishnan Venkiteswaran about 7 years
    Why is this the preferred answer, when the user has specified that it needs to work for .NET 4.0.
  • ErikusMaximus
    ErikusMaximus over 6 years
    Note that J# isn't shipped with .NET Framework anymore, although it is still shipped with VS2005-2015. This might still be a valid solution for some people. social.msdn.microsoft.com/Forums/office/en-US/…
  • Abbas
    Abbas over 3 years
    Its not the answer, Need to know how to do it with .NET < 4.5
  • Ivan Ičin
    Ivan Ičin over 3 years
    @Abbas, at the time the question was asked there was no .NET 4.5, .NET 4.0 was the latest version and you can interpret it as that, not as a specific version. As you can see above the answer on how to do that in .NET 4.5 was clearly useful to the OP. But you probably know better than OP what he meant and what is useful answer, right?