Finally! Simple Zip Functionality in .NET 4.5

Important: To use the ZipFile class, you must add a reference to the System.IO.Compression.FileSystem assembly in your project. You cannot use this class in Windows Store apps.

The methods for manipulating zip archives and their files are spread across three classes: ZipFile, ZipArchive and ZipArchiveEntry.

To create a zip archive from a directory, use ZipFile.CreateFromDirectory.
To extract the contents of a zip archive to a directory, use ZipFile.ExtractToDirectory.
To add new files to an existing zip archive, use ZipArchive.CreateEntry.
To retrieve an file in a zip archive, use ZipArchive.GetEntry.
To retrieve all of the files in a zip archive, use ZipArchive.Entries.
To open a stream to an individual file contained in a zip archive, use ZipArchiveEntry.Open.
To delete a file from a zip archive, use ZipArchiveEntry.Delete.

Single-line static methods:

using System.IO.Compression;

namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            string startPath = @"c:\example\start";
            string zipPath = @"c:\example\result.zip";
            string extractPath = @"c:\example\extract";

            ZipFile.CreateFromDirectory(startPath, zipPath);

            ZipFile.ExtractToDirectory(zipPath, extractPath);
        }
    }
}
Advertisements