C# - Zip Compression And Decompression .NET 4 And Earlier

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:  
  3. using (var zipFile = new ZipFile())  
  4. {  
  5.    // The empty string parameter ensures the file is archived to the   
  6.    // root of the archive  
  7.    zipFile.AddFile(@“C:\Test\Data.txt”, string.Empty);  
  8.    zipFile.Save(@“C:\Test\Data.zip”);  
  9. }  
  11. //To decompress files out of a zip archive:  
  13. using (var zipFile = new ZipFile(@“C:\Test\Data.zip”))  
  14. {  
  15.    zipFile.ExtractAll(@“C:\Test\Output”);  
  16. }