ZipFile class (System.IO.Compression.FileSystem)

I was excited that the base class library in .NET 4.5 now includes native support for working with Zip files, but in my excitement I overlooked System.IO.Compression.FileSystem. The name of the assembly is actually misleading, because the namespace is still just System.IO.Compression, and it only contains one type and that's ZipFile. This is a separate assembly from System.IO.Compression.dll so you will need to add it as an additional reference.

The ZipFile class is actually static, but it contains some four methods for quickly creating and reading zip files.

ZipFile.CreateFromDirectory(String, String)
ZipFile.ExtractToDirectory(String, String)
ZipFile.Open(String, ZipArchiveMode)
ZipFile.OpenRead(String)

ZipFile.CreateFromDirectory(String, String) is useful for quickly archiving entire directories. I could backup my documents for instance.

ZipFile.CreateFromDirectory(@"c:\users\danderson\documents", @"c:\users\danderson\documents\backup.zip");

I could do the reverse to restore my documents.

ZipFile.ExtractToDirectory(@"c:\users\danderson\documents\backup.zip", @"c:\users\danderson\documents");

There are some limitations however. One is that these methods will throw exceptions if the target path already exists when creating an archive, or a target directory exists when extracting files. If an exception is thrown, it will remain partially extracted however many files and folders were previously successful.

5 Comments

  1. Manoj

    Getting same error Exception calling “ExtractToDirectory” with “2” argument(s): “Number of entries expected in End Of Central Directory does not correspond to number of entries in Central Directory.” I am trying to unzip 30 gb file
    Please help

  2. Pingback: [RESOLVED]Namespace name 'ZipArchive' could not be found | ASP.NET MVC

  3. Pingback: [RESOLVED]Namespace name 'ZipArchive' could not be found | ASP Web Form Data Control

  4. Pingback: [RESOLVED]Namespace name 'ZipArchive' could not be found | ASP Questions & Answers

  5. Prabhakar

    hi,
    I am unziping a Zip file of size 4.8Gb and i get an error
    System.IO.InvalidDataException: Number of entries expected in End Of Central Directory does not correspond to number of entries in Central Directory.
    at System.IO.Compression.ZipArchive.ReadCentralDirectory()
    at System.IO.Compression.ZipArchive.get_Entries()
    at System.IO.Compression.ZipFileExtensions.ExtractToDirectory(ZipArchive sour
    ce, String destinationDirectoryName)
    at System.IO.Compression.ZipFile.ExtractToDirectory(String sourceArchiveFileN
    ame, String destinationDirectoryName, Encoding entryNameEncoding)
    at System.IO.Compression.ZipFile.ExtractToDirectory(String sourceArchiveFileN
    ame, String destinationDirectoryName)
    at ConsoleApplication1.Program.ExtractZipFileContents(String zipPath, String
    extractPath) in

    if you need I can share the code too for your simualtion.
    Let me know if there is any hot fix

Leave a Comment