Zip Archives in C#

Introduction

This article explains how to create and extract a Zip archive using the Zip File class. It Zip Archive compresses and decompresses files.

Features of System.IO.Compression and System.IO.Compression.FileSystem in type that now covers all our archive compression and decompression needs. Represents a package of compressed files in the Zip archive format. It compresses the contents of a folder into a Zip archive, and then extracts that content to a new folder.

Step 1: Open your instance of Visual Studio 2012, and create a new Windows application.

Name the project "ZipArchiveApplication", as shown in the following figure:

create a new Windows application

Step 2: Add a reference to the "System.IO.Compression" and "System.IO.FileSystem" namespaces:

Add Reference

Step 3:

//Using namespaces

using System.IO;

using System.IO.Compression;

Step 4: Zip Archive compress and decompress files.

    // This Code used Create Zip And ZipArchive Compression File       

        private void btnZipArchiveCompression_Click(object sender, EventArgs e)

        {

            string startPath = @"C:\sxs";

            string zipPath = @"C:\Zips\result.zip";

            //ZipFile.CreateFromDirectory(startPath, zipPath);

            ZipFile.CreateFromDirectory(startPath, zipPath, CompressionLevel.Fastest, true);

            MessageBox.Show("Zip file Create Successfully!! ");

        }   

        // This Code used Extract Zip and ZipArchive DeCompression File

        private void btnZipArchiveDeCompress_Click(object sender, EventArgs e)

        {

            string zipPath = @"C:\Zips\result.zip";

            string ExtractPath = @"C:\Zips\extract";

            ZipFile.ExtractToDirectory(zipPath, ExtractPath);

            MessageBox.Show("Zip file Extract Successfully!! ");

        }

Step 5: Now run the Applications, it will look like this:

run the Applications

A message window will show and Zip Show, it will look like this:

Message Windows

A message window will show and Zip File Extract, it will look like this:

Zip File Extract

I hope this article is useful. If you have any other questions then please provide your comments in the following.

Up Next
    Ebook Download
    View all
    Learn
    View all