Create Your Own ZIP Tool in C# Using Iconic.Zip.dll

Introduction

We have been using third-party compression tools for ages. Since we have WinZip and WinRAR in the top-selling market. Still, it’s a great way to have your own Zip compression tool.

Then, we have a few questions that come to mind.

"Are You Serious?", "I can create my own compression Tool?". And, the answer is "Yes, you can".

So, in this demonstration we will make our stand-alone Zip tool.

ZIP tool

Background

In background, we actually need a third-party library file (DLL). You can get that DLL file from DotNetZip with its samples and documentation.

And you need basic knowledge of C# coding to make your application work.

Download Iconic.Zip.dll : Ionic.Zip.dll

(The only necessary DLL file in this project.)
 
Procedures

Step 1: Create a Windows Forms project with your own name.

I named it Zipper. :D.

Add a Label, Text Box and Button,

Windows Form Control

This is the Browse Your File step, so that we can select our desired file.

This is to be zipped in the near future.

So, for this we defiantly need an "Open File Dialog Control" from the Tool box.

Open File Dialog Control

For now, a Button (Folder Icon) will raise the Open Dialog window.

And after selecting all the files from the Open File Dialog we put them in a ListBox.

So, let’s do this by code.

// Browse Button

        private void button1_Click(object sender, EventArgs e)

        {           

            if (openFileDialog1.ShowDialog() == DialogResult.OK)

            {

                BrowserTextBox.Enabled = true;

                BrowserTextBox.Text = openFileDialog1.FileName.ToString();             

                SelectedFileList.Items.Add(openFileDialog1.FileName);

            }

        }

Explanation

In the first if condition, we check if the "OK" Button of the OpenDialog1 window was pressed. If Yes, then go with the condition else you can move on.

If Yes then the only TextBox is enabled by assigning the true value.

By default we have:

TextBox Property

And then the Selected FileName with the TextBox is assigned.

Finally, we add this selected file to the ListBox using the Add() method.
 
Step 2: Next, we will code the Delete Button of the ListBox.

Delete Button of the ListBox

The only purpose of this Delete is to remove the selected List Item from the List Box.

So, do this via code as in the following:

//Delete Button

private void button1_Click_1(object sender, EventArgs e)

{

   SelectedFileList.Items.RemoveAt(SelectedFileList.SelectedIndex);

}
 
Explanation

There is nothing complicated in this code.

We are calling the SelectedFileList.RemoveAt() method that takes an index number as the argument.

For that, we called a List Property SlectedFileList.SelectedIndex that returns the current selected List Item.

Here:  SelectedFileList is the name of my ListBox.
 
Step 3: We will now use a Check Box, Text Box and a Button.

Where the Check Box activates the TextBox and Button that actually accepts the word.

 So, let’s do this:
 

// word Check Box

private void Isword_CheckedChanged(object sender, EventArgs e)

{

        if (Isword.Checked)

        {

            wordTextBox.Enabled = true;

            AddwordButton.Enabled = true;

        }

        else

        {

            wordTextBox.Enabled = false;

            AddwordButton.Enabled = false;

        }
 }

 
Here, Isword is the name of my Check Box Control.

Same with word TextBox and Add word Button.
 
And, when we press the Add_word Button then,

//word Button

private void AddwordButton_Click(object sender, EventArgs e)

{

    word = wordTextBox.Text;

    wordTextBox.Enabled = false;

}

 
It assigns the word Text Box value to a local word String variable.

And, Disable the word Text Box.
 
Add a Text Box for accepting Zip file name that we will use in future,

ZIP file name

Name it as ZipTextBox.

Step 4:
Now, we will ask from the user to set the Destination Folder.

For this, we use a Folder Dialog Control as in the following:

Folder Dialog Control

And, now we code the Browse Button as in the following:

Browse Button
 

//Destination Folder Button

private void button1_Click_2(object sender, EventArgs e)

{

        if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)

        {

            DestinationTextBox.Enabled = true;

            DestinationTextBox.Text=folderBrowserDialog1.SelectedPath.ToString();

        }

}
 
It looks similar to the File Browser Window what we had dealt with earlier in this project.

Finally, we assigned the Selected Folder destination to a Text Box.

In my case, it is Destination TextBox.
 
Step 5:


Window Form 
 
Next, we code our Zip button that actually calls a user-defined method to do the zip.
 

//Finally, ZIP Button

private void ZIPButton_Click(object sender, EventArgs e)

{

     foreach(object item in SelectedFileList.Items)

     listSelectedFile.Add(item.ToString());

 

    DestinationPath = DestinationTextBox.Text;

    ZipName = ZipTextBox.Text;

    status = CallZiper(listSelectedFile,word,DestinationPath,ZipName);

     if (status == true)

            MessageBox.Show("Successfully Zipped :)");

     else

          MessageBox.Show("Some Error Occurred :(");
}

 
In the first foreach() loop, we add the selected item to the generic list.

In the second and third statement, we obtained the DestinationPath and ZipName from the corresponding Textboxes.

In the last we call a special user defined method CallZiper() that returns a bool value.

It takes 4 arguments for processing.
 

public static bool CallZiper(List<string> listSelectedFile, string word, string DestinationPath, string ZipName)

{

      if (listSelectedFile != null)

     {              

           using (ZipFile zip = new ZipFile())

          {

                 zip.word = word;

                 foreach (var sFile in listSelectedFile)

                 {

                     zip.AddFile(sFile);

                 }

                 zip.Save(DestinationPath + "\\" + ZipName+".zip");

 

                 return true;

             }

         }

         else

            return false;
}

 
In the first if Condition, we check whether or not our generic list is NULL.
 
If it’s not NULL then create a ZipFile object inside the using statement. Or, you can use own your way.

Then, you need to close the Zip object.

Note: using Iconic.Zip;

Next assign the word property with the local word variable that we had already assigned earlier.

Similarly, we us the Add() method of the ZipFile object.

At last, we use the Save() method where we provide a Destination path of the final Zip file.
 
Output

Output
 
As in you get this:

Zip File

Conclusion

If you encounter any issue with the code then you can refer to the enclosed code with it.


Similar Articles