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 is used to create a Zip and ZipArchive compressed 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 created successfully!!");
}
// This code is used to extract a Zip and ZipArchive decompressed 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 extracted successfully!!");
}

Step 5. Now run the Applications. It will look like this.

run the Applications

A message window will show, and Zip Show 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.


Similar Articles