Compress & Zip files using DotNetZip

0.00 avg. rating (0% score) - 0 votes

Introduction

Have you ever encountered a scenario when you have to download few files zipped and compressed! Few development involving the manipulation of documents and its management would require this. There are a lot of packages out in the market. Here in this article, I would be sharing the use of DotNetZip package used to zip, unzip & compress files using C#, VB.Net & any .NET language.

Practicality

Download the DotNetzip package from Nuget package.
Download
Once downloaded, its all set to use the dotnetzip package to start zipping the files and compressing them. For the files to be zipped, here I will be using the file path and select each file to be zipped. Here also we will see how a file being created on the fly (a pdf using rotativa) is saved in same folder and zipped.

File created on the fly using Rotativa

The above code snippet is generating a PDF using a cshtml Razor view page using Rotativa Using Rotativa Best to follow the mentioned article for more information to generate PDF using Rotativa using MVC.
Lets look at the code snippet for the Zipping.

The above are the three algorithms used. I personally have used only BZip2 based on few good reviews.
Once compressed and all files inserted into the folder, the zipped folder is ready to be downloaded using the FileStreamResult in MVC action.

Conclusion

This is the simple explanation and the code snippet for the Zip file concept. This is simple and really handy to be used and it also provides the compression algorithms which are good to go with.