This is a quick post about a useful third-party zip compression library if you’re working in .NET framework version 4 or earlier.
I had been working on a SQL Server 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.
  1. //To compress files out of a zip archive:
  2. using (var zipFile = new ZipFile())
  3. {
  4. // The empty string parameter ensures the file is archived to the
  5. // root of the archive
  6. zipFile.AddFile(@“C:\Test\Data.txt”, string.Empty);
  7. zipFile.Save(@“C:\Test\Data.zip”);
  8. }
  9. //To decompress files out of a zip archive:
  10. using (var zipFile = new ZipFile(@“C:\Test\Data.zip”))
  11. {
  12. zipFile.ExtractAll(@“C:\Test\Output”);
  13. }