32

Is there a built-in zip library in .NET 3.5?

If not, what are the popular open source .net zip libraries.

Oscar Cabrero
  • 4,168
  • 8
  • 29
  • 49
Guy
  • 65,082
  • 97
  • 254
  • 325

9 Answers9

32

EDIT: See note in comments - SharpZipLib is now unmaintained, and you probably want to avoid it.

Open source: #ZipLib

I believe that the classes in the System.IO.Compression namespace are fine for compressing/decompressing a single stream of data, but there's nothing built into the framework to cope with actual zip files.

EDIT: As noted in Ants' answer, there's System.IO.Packaging.ZipPackage but it certainly looks like that's really designed for use in WPF, and wouldn't be terribly convenient to use for general zip file handling. Worth looking into though. I wasn't aware of it before though... definitely worth investigating.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • isn't c# 4.0 going to natively support this? i thought I had read that somewhere. But I have used ZipLib before with success. – Jon Erickson Feb 27 '09 at 00:14
  • I don't know whether .NET 4.0 will have support for this - I wouldn't be surprised. It's still a way off though :) – Jon Skeet Feb 27 '09 at 07:32
  • I found that the SharpZipLib API can be more complicated than it needs to be. Also it is much more than zip - tar, bzip, and i don't know what else. Maybe "too much" if you just want zip. Last, are some things missing? like progress events? AES? maybe these are coming. I'm not aware. – Cheeso Mar 09 '09 at 22:06
  • SharpZipLib is unmaintained and buggy. Last release in June 2010. It sometimes has issues with zip files produced by 7zip, ioniczip, etc, even though the zip files are completely fine. We use it for a component at my work and have had to resort to fixing the source and building locally, which is not that big of a deal, but not something you want to have to get into if you are looking for a turnkey zip library. – aggieNick02 Jan 11 '13 at 17:25
  • @aggieNick02: I'll add a note to the answer. – Jon Skeet Jan 11 '13 at 17:58
  • 1
    I'm not sure how `ZipPackage` would be inconvenient. All you need is a library which allows adding files to a zip and retrieving from a zip. How complex could you possibly need it? Also can you elaborate on why it looks like it's for WPF? – The Muffin Man Oct 30 '14 at 21:09
  • 2
    @TheMuffinMan: The problem is that `ZipPackage` *is* complex, compared with that set of requirements - relationships, parts etc. It's part of a bigger packaging framework. As for the relationship to WPF - that was mostly on the basis of what else is in `WindowsBase.dll`, most of which seems to be WPF-related. – Jon Skeet Oct 30 '14 at 21:44
23

There is no built-in library. There are open-source options.

DotNetZip is one. Simple, easy to use. It has good features: AES Encryption, regular encryption, streams, ZIP64, file comments, Unicode, progress events, more. And it's free and open source.

Here's some sample code.

    // extract all Photoshop files larger than 100mb
    using (ZipFile zip1 = ZipFile.Read(ZipFileName))
    {
        var LargePhotoShopFiles = zip1.SelectEntries("name = *.psd  and size > 100mb");
        foreach (ZipEntry e in LargePhotoShopFiles)
        {
            if (e.UsesEncryption)
                e.ExtractWithPassword("unpackDirectory", "VerySecret!");
            else 
                e.Extract("unpackDirectory");
        }
    }
Cheeso
  • 189,189
  • 101
  • 473
  • 713
  • 1
    I'm very impressed with this library. Using it with great success now - thanks for the heads up. – Guy Mar 06 '09 at 17:31
  • @Chesso, from 3.5 you have new zip library at http://msdn.microsoft.com/en-us/library/system.io.packaging.zippackage.aspx – Avram Feb 06 '11 at 09:55
  • 1
    Avram, System.IO.Packaging is not a real zip library. – Cheeso Feb 08 '11 at 04:01
  • @Cheeso How is it not a 'real' zip library? What doesn't it do that is expected of any zip library? – The Muffin Man Oct 30 '14 at 21:06
  • 1
    Looks like the original source that's linked to in this answer is a bit stale now, with the last update being July '11. Here's a fork of it that has newer bug fixes and is available through NuGet: https://github.com/haf/DotNetZip.Semverd – HotN Jan 20 '15 at 19:18
10

Check out System.IO.Packaging.ZipPackage class.

Ants
  • 2,628
  • 22
  • 35
  • 5
    This requires things to be in a package format which requires an XML file inside the zip file that defines the layout. It's not a general purpose zip library. – Jeff Moser Mar 02 '09 at 22:14
  • There are problems to find it on standard reference .Net lib, ( http://blogs.msdn.com/b/dmahugh/archive/2006/12/14/finding-windowsbase-dll.aspx ) – Avram Feb 06 '11 at 09:56
6

7Zip will help and its available in multiple languages

Oscar Cabrero
  • 4,168
  • 8
  • 29
  • 49
  • The LZMA SDK you linked to is does not read or write ZIP files. It might be possible to use the 7Zip DLLs, but I'm not sure if there's documentation on that... – sourcenouveau Jan 27 '11 at 18:42
4

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

Paul Rowland
  • 8,244
  • 12
  • 55
  • 76
1

System.IO.Compression has ZipArchive class as of .Net 4.5.

Arthur Stankevich
  • 372
  • 1
  • 4
  • 11
1

Try System.IO.Compression.DeflateStream.

Inferis
  • 4,582
  • 5
  • 37
  • 47
  • 4
    No, DeflateStream doesn't do ZIP files. Just FYI, neither does the built-in GZipStream. Also, both these streams can exhibit anomalous compression behavior with previously-compressed or incompressible data. They can actually increase the size of the data. you've been warned! – Cheeso Mar 02 '09 at 22:12
1

I will be second to recommend http://www.7-zip.org/sdk.html LZMA SDK, but it's not ZIP.

  1. It's in public domain
  2. It's FAST on decompression
  3. It has fully managed implementation
  4. It's much better compressing than ZIP/RAR
  5. It has very small footprint
  6. It can work as a stream
Mash
  • 877
  • 6
  • 16
  • Doesn't answer the request. The question was about zip libraries, not alternatives to zip libraries. In many case the specs are set for you, or you are working with a third party component which produces a certain spec. – Kir Aug 27 '13 at 16:15
0

You can extract a zip using this:

    public static void UnZip(string zipFile, string folderPath)
    {
        if (!File.Exists(zipFile))
            throw new FileNotFoundException();

        if (!Directory.Exists(folderPath))
            Directory.CreateDirectory(folderPath);


        var t = Type.GetTypeFromProgID("Shell.Application");

        dynamic shellApplication = Activator.CreateInstance(t);

        dynamic destinationFolder = shellApplication.NameSpace(folderPath);
        dynamic sourceFile = shellApplication.NameSpace(zipFile);

        foreach (var file in sourceFile.Items())
        {
            destinationFolder.CopyHere(file, 4 | 16);
        }
    }
John
  • 5,942
  • 3
  • 42
  • 79