All About Zip Files in C#

Step 1

The first step is to download the DotNetZip Library in your project from the Nuget Manager:

DotNetZip

After doing that, you would need to now include the DLL in your C# code as well, using the following namespace.

  1. using Ionic.Zip;  
We will now explain two separate functionalities of this library, in other words displaying files from Zip files and downloading multiple files as Zip.

Displaying Files from Zip files

To proceed with this, let's make a sample Zip file and include it in the root of our project. The following is the structure of the Zip file:

Add the reference

Now, let's start reading the files one by one and display their names in a GridView.

generate the PDF

Code Description: The code reads each file from the path provided and provides the name and the size of each of them. It is then displayed in a GridView as in the following:

Code for add text object

There are a few more parameters that we can get for the file, like Compression Method, CompressedTime and so on.

Downloading multiple files as Zip

To get this, we first display the files located on the server in a GridView. By doing this, we are providing the functionality to the user to select the files that needs to be downloaded.

We first store some files in a new folder named “Files” located at the root of the project.

Then we write the following mentioned code to get this in a GridView along with a checkbox using which the file(s) can be selected.

The code is as in the following:

Creating PDF with image

The code just reads the file inside a location and stores the name in a generic list.

The output of that is:

Output

Now, in the next step we will write the code to get all the Selected files downloaded in the Zip format.

Creating word protected PDF

The preceding code will be executed on the click of the Download button and will download the selected files in the Zip format.

I have also attached the project to this article. You can download it for the code.

Feel free to comment.
Thanks,
Vipul


Similar Articles