Coder Perfect, you may unzip files programmatically.


I’m attempting to unzip a zipped file programmatically.

I tried using the System.IO.Compression.GZipStream class in.NET, but I get the following problem when my app (which is actually a unit test) runs:

I now understand that file is distinct from a.gz file, and that GZip is distinct from Zip.

However, because I can manually extract the file by double-clicking it and then hitting the “Extract all files” button, I believe there should be a mechanism to do it in code as well.

As a result, I tried calling Process.Start() with the location to the zipped file as an argument. This causes my software to display the contents of the compressed file in a window. That’s OK, however the software will be placed on a server where no one will be available to press the “Extract all files” button.

So, how do I get my app to open the zipped files and extract the files?

Is there a different way to go about it? I prefer to accomplish things in code, rather than using third-party libraries or apps, because the security department isn’t too keen on that…

Asked by Petteri

Solution #1

You may now use the.NET framework to unzip files with.NET 4.5:

using System;
using System.IO;

namespace ConsoleApplication
  class Program
    static void Main(string[] args)
      string startPath = @"c:\example\start";
      string zipPath = @"c:\example\";
      string extractPath = @"c:\example\extract";

      System.IO.Compression.ZipFile.CreateFromDirectory(startPath, zipPath);
      System.IO.Compression.ZipFile.ExtractToDirectory(zipPath, extractPath);

The following code was directly copied from Microsoft’s documentation:

ZipFile is part of the System.IO.Compression.FileSystem component. (Many thanks to nateirvin…see his comment below.) The framework assembly System.IO.Compression.FileSystem.dll requires a DLL reference.

Answered by bsara

Solution #2

For .Net 4.5+

Writing the uncompressed file to disk is not always desirable. As an ASP.Net developer, I’d have to mess about with permissions to give my program permission to write to the filesystem. I can avoid all of this by working with streams in memory and reading the files directly:

using (ZipArchive archive = new ZipArchive(postedZipStream))
    foreach (ZipArchiveEntry entry in archive.Entries)
         var stream = entry.Open();
         //Do awesome stream stuff!!

Alternatively, you can call ExtractToFile() to write the decompressed file to disk:

using (ZipArchive archive = ZipFile.OpenRead(pathToZip))
    foreach (ZipArchiveEntry entry in archive.Entries)
        entry.ExtractToFile(Path.Combine(destination, entry.FullName));

You must include a reference to the System.IO.Compression namespace as well as System.IO.Compression.FileSystem in order to use the ZipArchive class.

Answered by Mister Epic

Solution #3

SharpZipLib has been used effectively on a number of projects. Although it is a third-party tool, the source code is supplied and may provide some insight if you decide to reinvent the wheel.

Answered by Chris Conway

Solution #4

There are no external DLL files and it is free. Everything is contained in a single CS file. One download has only the CS file, while the other contains a simple example. I just tried it out today and was blown away by how easy it was to set up. It worked on the first try, with no mistakes.

Answered by Lukas

Solution #5

Visit to get the DotNetZip library.

Answered by Sam Axe

Post is based on