Friday 19 December 2014

DotNetZip - Zip Compression and Decompression .NET 4 and Earlier

Quick post about a useful third-party zip compression library if you're working in .NET framework version 4 or earlier.

I am working on a SQL Server 2012 Integration Services (SSIS) package. One of the steps in the package is to decompress a set of zip archives. Unfortunately, .NET script tasks in the SSIS package can only target .NET framework version 4 and earlier. This means that I couldn't make use of the new zip compression classes introduced in .NET 4.5 (see System.IO.Compression).

Fortunately though, there are a handful of open source .NET zip libraries available. The one I opted for is called DotNetZip. DotNetZip has an intuitive API and is working well with a large number of files (I am decompressing approximately 15,000 archives). The library is available as a NuGet package. The two snippets below show just how easy it is to compress and decompress files using zip.

To compress a file into a zip archive:
using (var zipFile = new ZipFile())
{
    // The empty string parameter ensures the file is archived to the 
    // root of the archive
    zipFile.AddFile(@"C:\Test\Data.txt"string.Empty);
    zipFile.Save(@"C:\Test\Data.zip");
}

To decompress files out of a zip archive:
using (var zipFile = new ZipFile(@"C:\Test\Data.zip"))
{
    zipFile.ExtractAll(@"C:\Test\Output");
}

No comments:

Post a Comment