Create ZIP (System.IO.Compression) From WinForm Application

In this article, you will learn the following things.

  • What is a zip file?
  • What is a zip utility?
  • How to create zip files pragmatically (Step by step implementation)

    Using Dot Net Inbuilt (System.IO.Compression) 

What is a Zip File?

Often, the files with zip extension are compressed files. We can create a zip file using various programs (UI) or via the command utility; such as -

Program UIs

  • WinZip
  • WinRar
  • 7ZIP

Command Line Utilities

  • PkZip
  • Rar
  • Arj

There are various ZIP utilities which compress our one or more files into a single ZIP file.

.NET

(Logo copied from www.winzip.com for display and educational purpose)

As you can see in the above WinZip logo icon, a compressing machine is compressing the files and folders. Usually, the ratio of text, doc, and XLS files compression is way higher than the images and video files.

ZIP Utility

In old days, developers used third-party utilities or drivers to create a zip file programmatically, such as -

  1. DotNetZip - Free Utility
  2. SharpZipLib - Free Utility
  3. Component Pro - Paid

But then, Microsoft introduced a new namespace called System.IO.Compression in .NET 4.5 and above frameworks. Using this namespace, we can create a zip file very easily.

This namespace has the following utility and commands.

ZipFile a is static class which has the following methods,

  • ZipFile.CreateFromDirectory - To create a zip file from a given directory/folder path.
  • ZipFile.ExtractToDirectory - To extract a zip file into the selected directory/folder path.

In this article, we are going to create a zip file by using Windows.Forms application.

Let's start step by step.

STEP 1

Create a Windows.Forms application called “ZipWinFormApp”.

.NET

STEP 2

Double-click on Form1.CS file in Solution Explorer. You can switch ON the Solution Explorer by pressing "CTRL + ALT + L" or  "ALT + V + P".

.NET

Now, again, double-click on the canvas of Form1.cs file. You can see Form1_Load method gets opened by-default. Let's start writing the code inside this method.

  1. private void Form1_Load(object sender, EventArgs e) {  
  2.     //this stand for current form  
  3.     //Text property to assign title(name) which display while we execute the program.         
  4.     this.Text = "Folder Compression Utility";  
  5. }  

Now, right-click on References folder and select Assemblies >> Framework. Select the following two libraries.

  1. System.IO.Compression
  2. System.IO.Compression.FileSystem

.NET

And add the followings namespace to Form1.cs on the top.

  1. using System.IO;  
  2. using System.IO.Compression;  

Go through the following links of MSDN pages to learn more.

STEP 3

Now, switch again to Form1.cs file and drag and drop the following items on Form1.cs canvas.

SR.NO.CONTROL NAMEDESCRIPTION
1FolderBrowserDialogThis controls how to browse and select the folder. 
2TextBoxThis controls where a user can write the path or selected folder path with the help of the browser. The dialog will be auto-posted on this text box. 
3ButtonTwo buttons are required: 1. The first one is to execute/fire the folder browse and selection dialog box. 2. The second button is used to start and perform the zip process. 

FolderBrowserDialog

This dialog box is used to browse and select the folder to further processing. In the following image, as you can see, this control is easily available inside Dialogs option.

.NET

You can easily get TextBox and Button control inside Common controls and All Windows.Forms tab.

Now, it's time to give naming to our following controls and titles.

Select TextBox and right click on TextBox, select Property option.

SR.NO.CONTROL NAMEPROPERTY DESCRIPTION and VALUE
1TextBox(NAME) = txtFolderPath (NAME) = this is the naming property of the control. txtFolderPath is the value or name of the textbox. By default, Visual Studio gives TEXTBOX1 as the name to the textbox.
2Button11. (NAME) = btnBrowse2. Text = Select Folder, stretch the button horizontally or increase the width of the button so that the title can be visible easily.
3Button21. (NAME) = btnZip2. Text = Create Zip

Now, let us start coding the buttons to implement the functionality.

On Browse Button (btnBrowse), write the following code.

  1. private void btnBrowse_Click(object sender, EventArgs e) {  
  2.     //To display/show dialogbox  
  3.     DialogResult result = folderBrowserDialog1.ShowDialog();  
  4.     //to check user pressed OK button or CANCEL button in show dialog  
  5.     if (result == DialogResult.OK) {  
  6.         //Paste the selected folder path to textbox  
  7.         txtFolderPath.Text = folderBrowserDialog1.SelectedPath;  
  8.     }  

On Create Zip button (btnZip), write this code.

  1. private void btnZip_Click(object sender, EventArgs e) {  
  2.     //to store the value of folderapth   
  3.     string FolderPathToZip = txtFolderPath.Text.Trim();  
  4.     //To create unique file name with date and time with nanoseconds.  
  5.     string ZipFileName = "D:\\backup\\bak-" + DateTime.Now.ToString("ddMMyyyy-HHmmssfffff") + ".zip";  
  6.     try {  
  7.         //To check whether D:\Backup folder exists or not.  
  8.         //If not exists this will create a BACKUP folder.  
  9.         if (Directory.Exists("D:\\backup")) {} else {  
  10.             Directory.CreateDirectory("D:\\backup");  
  11.         }  
  12.         //TO create a zip file.  
  13.         ZipFile.CreateFromDirectory(FolderPathToZip, ZipFileName);  
  14.     } catch (Exception) {  
  15.         //If system throw any exception message box will display "SOME ERROR"  
  16.         MessageBox.Show("Some Error");  
  17.     }  
  18.     //Display successfully created message to the user.  
  19.     MessageBox.Show("Zip Filename : " + ZipFileName + " Created Successfully");  

OUTPUT

Start the project by pressing F5.

.NET

Click on "Select Folder" button.

.NET

After folder selection,

.NET

As I clicked on Create Zip button.

.NET

You can see in above images, I have selected the path as C:\Users\admin\Desktop\MyArticles. The Zip file will be created inside d:\Backup folder.

The Zip file will be created every time with date and time stamp in the network. Any user can take backup of the data file or folder very easily without any overwriting or any problem.

.NET

Thank you … 

Happy coding…