Using the new ZipArchive class

We have two new classes, ZipArchive and ZipArchiveEntry, which add the capability to create .zip archives to the .NET framework. This was possible previously, but with several limitations.

How to do it...

Next we will see how to use the ZipArchive class to create a Zip file and to extract it to a concrete location.

  1. Create a new Visual Studio project of type Console Application named caZipArchive.
  2. Add the System.IO.Compression and System.IO.Compression.Filesystem assemblies as references to the project.
  3. Open the Program.cs file and add the following using clauses:
    using System.IO;
    using System.IO.Compression;
  4. Next, add the following method:
    static void CreateZipFile() {
    
        String ZipPath= @"C:PACKT2-CLRcaZipArchive	est";
        String ZipFilePath = ZipPath + "test.zip";
        String FileName01 = "OneTextDocument.txt";
        String FileName02 = "OtherTextDocument.txt";
        String FileToZip01 = ZipPath + FileName01;
        String FileToZip02 = ZipPath + FileName02;
    
    using (FileStreamZipToOpen = new FileStream(ZipFilePath, FileMode.CreateNew))
        {
         using (ZipArchiveZArchive = new ZipArchive(ZipToOpen,ZipArchiveMode.Create))
            {
           ZArchive.CreateEntryFromFile(FileToZip01, FileName01);
           ZArchive.CreateEntryFromFile(FileToZip02, FileName02);
            }
        }
    }
  5. We should change the directory address and create the two files at the corresponding folder, just for testing purposes.
  6. Call it from the Main method and execute the application. We should see that a Zip file has been created for us.
  7. Delete the Zip File.
  8. Add the following method:
    static void ExtractZipFile() {
        String ZipPath = @"C:PACKT2-CLRcaZipArchive	est";
        String ZipFilePath = ZipPath + "test.zip";
        String ExtractionPath = @"C:PACKT2-CLRcaZipArchive	estunzip";
    
    using (ZipArchiveZArchive = ZipFile.OpenRead(ZipFilePath))
        {
    foreach (ZipArchiveEntry zaEntry in ZArchive.Entries)
            {
          zaEntry.ExtractToFile(Path.Combine(ExtractionPath, zaEntry.FullName));
            }
        }         
    }
  9. And call it in the Main method, just after CreateZipFile:
    static void Main(string[] args)
    {
        //First we create the zip file
    CreateZipFile();
    
        //Next, we extract it
    ExtractZipFile();
    }
  10. Delete the previously generated ZIP file and create the unzip directory.
  11. If we execute the application, a ZIP file should be generated on the specified directory, and we should find all the files that we added in the extraction directory.

How it works...

We used FileStream to create a new file stream and write to it with ZipArchive.

Additionally, we added two files using the CreateEntryFromFile extension method of the ZipArchive class that is the result of adding the System.IO.Compression.FileSystem assembly.

With this we had our ZIP file created.

Continuing, we opened our ZIP file using the OpenRead method of the ZipFile class, which returns a ZipArchive object that represents the package of compressed files.

We can iterate all the entries with a simple foreach instruction. For each ZipArchiveEntry, we extract it with the ExtractToFile extension method.

And that's it! In a few lines of code we have created a ZIP file, added some files, and then extracted the ZIP files to another folder.

There's more...

We could also specify the level of compression, edit the files within the ZIP file, or update existing ZIP files.

Additionally, for simplicity we 'neither validated for the existence of files or directories nor checked for any errors, so we should do this properly in a production environment.

We should be aware that the extension methods (provided by the System.IO.Compression.FileSystem assembly) that we used aren't available in Windows Store apps. There we should compress and decompress using the GZipStream or DeflateStream class.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset