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.

28 Comments

  1. Pingback: Wholesale NFL Jerseys Cheap

  2. Pingback: Cheap NFL Jerseys From China

  3. Pingback: Wholesale Jerseys

  4. Pingback: Wholesale NFL Jerseys

  5. Pingback: Cheap NHL Jerseys

  6. Pingback: Cheap Nike NFL Jerseys

  7. Pingback: Umanizzare Gestão Prisional Ltda

  8. Pingback: Umanizzare presidios

  9. Pingback: Umanizzare Gestão Prisional Ltda

  10. Pingback: Egli Diana Pinto

  11. Pingback: Grupo Serval presidios

  12. Pingback: Cheap NFL Jerseys China Wholesale

  13. Pingback: Lelio Vieira Carneiro

  14. Pingback: Cheap Jerseys China Supply

  15. Pingback: Luiz Gastão Bittencourt

  16. Pingback: Lelio Junior

  17. Pingback: Cheap Jerseys China Online Store

  18. Pingback: Egli Diana Pinto

  19. Pingback: Umanizzare Gestão Prisional Ltda

  20. Pingback: Lelio Vieira Carneiro

  21. Pingback: Lelio Vieira Carneiro

  22. Pingback: Cheap NFL Jerseys USA

  23. Pingback: Itamar Serp

  24. 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

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

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

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

  28. 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

    Reply

Leave a Comment

Your email address will not be published. Required fields are marked *

%d bloggers like this: