Zip(Archive) API’s in .NET Framework 4.5 – Part 2 – ZipFile Class

In my previous post I shared some information on API’s/Classes included as part of System.IO.Compression namespace in .NET Framework 4.5, and given on overview of ZipArchive class. Once such class I would be sharing some insight with post today would be ‘ZipFile‘ class. The ZipFile class provides convenient static methods for working with zip archives:

  1. CreateFromDirectory (3 overloads) – Creates a zip archive that contains the files and directories from the specified directory, uses the specified compression level and character encoding for entry names, and optionally includes the base directory.
  2. ExtractToDirectory ( 3 overloads) – Extracts all the files in the specified zip archive to a directory on the file system and uses the specified character encoding for entry names.
  3. Open (2 overloads) – Opens a zip archive at the specified path, in the specified mode, and by using the specified character encoding for entry names.
  4. OpenRead  – Opens a zip archive for reading at the specified path.

 

To use these methods, you must reference the System.IO.Compression.FileSystem assembly in your project.

image

NB:

  • The System.IO.Compression.FileSystem assembly is not available for Windows Store apps. Therefore, the ZipFile class and ZipFileExtensions class (which is also in the System.IO.Compression.FileSystem assembly) are not available in Windows Store apps.
  • In Windows Store apps, you work with compressed files by using the methods in the ZipArchive, ZipArchiveEntry, DeflateStream, and GZipStream classes.

 

Now the time for creating some sample application.

For the purpose of explaining how to use the above methods ā€“ I followed the below steps :

  1. Created a console application in visual studio.
  2. Added Reference to System.IO.Compression and System.IO.Compression.FileSystem assemblies which is part of .NET Framework 4.5.
  3. Created a folder called ā€œFilesā€ and created some plain text files(.txt extension) , this would be out source folder to zip. And created two additional folder for storing zip files(Output) and ExtractLocation folder to extract the zip files.
  4.  image image
  5. and the below sample code snippet is the implementation/usage.
<code>&lt;p&gt;Sample code snippet:&lt;/p&gt; &lt;div&gt; &lt;pre class="brush: c#;"&gt;
</code>

namespace ConsoleApp02
{
class Program
{
static void Main(string[] args)
{
// AppDomain.CurrentDomain.BaseDirectory refers to the
//folder in which the executable or binaries are executing.
// E.g. ConsoleApp02ConsoleApp02binDebug
string startPath = AppDomain.CurrentDomain.BaseDirectory + "\Files";
string zipPath = AppDomain.CurrentDomain.BaseDirectory + "\Output";
string zipFilePath = zipPath + "\" + System.Guid.NewGuid().ToString() + ".zip";

<code>        string extractPath = AppDomain.CurrentDomain.BaseDirectory + &amp;quot;\ExtractLocation&amp;quot;;

        //just a fail-safe to create folders if not exists.
        if (!Directory.Exists(zipFilePath))
            Directory.CreateDirectory(zipFilePath);

        if (!Directory.Exists(extractPath))
            Directory.CreateDirectory(extractPath);

        //Creating a zipFile from folder
        ZipFile.CreateFromDirectory(startPath, zipFilePath);

        //Unzipping a zipFile to a folder
        ZipFile.ExtractToDirectory(zipFilePath, extractPath);

        //
        Console.WriteLine(&amp;quot;Press Any key to exit...&amp;quot;);

        Console.ReadKey();
    }
}
</code>

}

Hope this post is informative. Please keep share this post and give your comments/feedback. Happy coding!