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
Command Line Utilities
There are various ZIP utilities which compress our one or more files into a single ZIP file.
(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 -
- DotNetZip - Free Utility
- SharpZipLib - Free Utility
- 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”.
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".
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.
- private void Form1_Load(object sender, EventArgs e) {
-
-
- this.Text = "Folder Compression Utility";
- }
Now, right-click on References folder and select Assemblies >> Framework. Select the following two libraries.
- System.IO.Compression
- System.IO.Compression.FileSystem
And add the followings namespace to Form1.cs on the top.
- using System.IO;
- 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 NAME | DESCRIPTION |
1 | FolderBrowserDialog | This controls how to browse and select the folder. |
2 | TextBox | This 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. |
3 | Button | Two 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.
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 NAME | PROPERTY DESCRIPTION and VALUE |
1 | TextBox | (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. |
2 | Button1 | 1. (NAME) = btnBrowse2. Text = Select Folder, stretch the button horizontally or increase the width of the button so that the title can be visible easily. |
3 | Button2 | 1. (NAME) = btnZip2. Text = Create Zip |
Now, let us start coding the buttons to implement the functionality.
On Browse Button (btnBrowse), write the following code.
- private void btnBrowse_Click(object sender, EventArgs e) {
-
- DialogResult result = folderBrowserDialog1.ShowDialog();
-
- if (result == DialogResult.OK) {
-
- txtFolderPath.Text = folderBrowserDialog1.SelectedPath;
- }
- }
On Create Zip button (btnZip), write this code.
- private void btnZip_Click(object sender, EventArgs e) {
-
- string FolderPathToZip = txtFolderPath.Text.Trim();
-
- string ZipFileName = "D:\\backup\\bak-" + DateTime.Now.ToString("ddMMyyyy-HHmmssfffff") + ".zip";
- try {
-
-
- if (Directory.Exists("D:\\backup")) {} else {
- Directory.CreateDirectory("D:\\backup");
- }
-
- ZipFile.CreateFromDirectory(FolderPathToZip, ZipFileName);
- } catch (Exception) {
-
- MessageBox.Show("Some Error");
- }
-
- MessageBox.Show("Zip Filename : " + ZipFileName + " Created Successfully");
- }
OUTPUT
Start the project by pressing F5.
Click on "Select Folder" button.
After folder selection,
As I clicked on Create Zip button.
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.
Thank you …
Happy coding…