Extract a ZIP file programmatically by DotNetZip library?

Ehsan picture Ehsan · Feb 24, 2010 · Viewed 45.8k times · Source

I have a function that get a ZIP file and extract it to a directory (I use DotNetZip library.)

public void ExtractFileToDirectory(string zipFileName, string outputDirectory)
{
     ZipFile zip = ZipFile.Read(zipFileName);
     Directory.CreateDirectory(outputDirectory);
     zip.ExtractAll(outputDirectory,ExtractExistingFileAction.OverwriteSilently);
}

My ZIP file contains multiple files and directories. But I want to extract only some of these files, not all of them.

How can I make this work?

Answer

Oded picture Oded · Feb 24, 2010

You need to test each ZipEntry to see if you want to extract it:

public void ExtractFileToDirectory(string zipFileName, string outputDirectory)
{
     ZipFile zip = ZipFile.Read(zipFileName);
     Directory.CreateDirectory(outputDirectory);
      foreach (ZipEntry e in zip)
      {
        // check if you want to extract e or not
        if(e.FileName == "TheFileToExtract") 
          e.Extract(outputDirectory, ExtractExistingFileAction.OverwriteSilently);
      }
}