Extract a ZIP file programmatically by DotNetZip library?

There is a ExtractSelectedEntries method in ZipFile class. here's the method signature.

public void ExtractSelectedEntries(string selectionCriteria, string directoryPathInArchive, string extractDirectory, ExtractExistingFileAction extractExistingFile)

So in your program, you can simply extract the specified files by providing the selectionCriteria.

public void ExtractFileToDirectory(string zipFileName, string outputDirectory)
{
     ZipFile zip = ZipFile.Read(zipFileName);
     Directory.CreateDirectory(outputDirectory);
     zip.ExtractSelectedEntries("name = *.doc", "document\", outputDirectory, ExtractExistingFileAction.OverwriteSilently);
}

You can combine criteria with the conjunctions AND or OR. Using a string like "name = *.txt AND size >= 100k" for the selectionCriteria retrieves entries whose names end in .txt, and whose uncompressed size is greater than or equal to 100 kilobytes.

here are some criteria samples


criteria (Files retrieved)

name != *.xls (any file with an extension that is not .xls)

name = *.mp3 (any file with a .mp3 extension)

*.mp3 (same as above, any file with a .mp3 extension)

attributes = A (all files whose attributes include the Archive bit)

attributes != H (all files whose attributes do not include the Hidden bit)

mtime > 2009-01-01 (all files with a last modified time after January 1st, 2009)

size > 2gb (all files whose uncompressed size is greater than 2gb)


For more reference, you should read the API document alone with the library.


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);
      }
}