How to Compress a File on Upload & Download the Zip File

Introduction

This article describes how to compress a file into Zip format when it is uploaded and download the Zip file.

Description

To create this application you need a third-party DLL file to be referenced by the application:
  • Ionic.Zip.dll

You can download it from the source code attached to this page or from the following link:
http://dotnetzip.codeplex.com/

Design

Add a FileUpload Control, a button and a GridView.

Now design your screen as in the following screen.

1.png

Or you can copy the following source code:

 

  1. <form id="form1" runat="server">  
  2.    <div>  
  3.         <table align="center">  
  4.             <tr>  
  5.                 <td align="center">  
  6.                     <asp:FileUpload ID="FileUpload1" runat="server" />  
  7.                 </td>  
  8.             </tr>  
  9.             <tr>  
  10.                 <td align="center">  
  11.                     <asp:Button ID="Button1" runat="server" Text="Save and compress" OnClick="Button1_Click" />  
  12.                 </td>  
  13.             </tr>  
  14.             <tr>  
  15.                 <td align="center">  
  16.                     <asp:GridView ID="gvZip" runat="server" AutoGenerateColumns="false" OnRowCommand="gvZip_RowCommand">  
  17.                         <Columns>  
  18.                             <asp:TemplateField HeaderText="Click to Download">  
  19.                                 <ItemTemplate>  
  20.                                     <asp:LinkButton ID="lnkDownload" runat="server" Text='<%# Bind("Name") %>' CommandName="Download"  
  21.                                         CommandArgument='<%# Bind("Name") %>' />  
  22.                                 </ItemTemplate>  
  23.                             </asp:TemplateField>  
  24.                         </Columns>  
  25.                     </asp:GridView>  
  26.                 </td>  
  27.            </tr>  
  28.         </table>  
  29.     </div>  
  30. </form> 

Now go to the code view.

Next add a reference of the following  DLL to your website:

  1. Ionic.Zip.dll

And write the following code in the .cs file:

  1. using System;    
  2. using System.Collections;    
  3. using System.Linq;    
  4. using System.Web;    
  5. using System.Web.UI;    
  6. using System.Web.UI.WebControls;    
  7. using Ionic.Zip;    
  8. using System.IO;    
  9. public partial class _Default : System.Web.UI.Page    
  10. {    
  11.     protected void BindDataList()    
  12.     {    
  13.         DirectoryInfo dir = new DirectoryInfo(MapPath("Zip"));    
  14.         FileInfo[] files = dir.GetFiles();    
  15.         ArrayList listItems = new ArrayList();    
  16.         foreach (FileInfo info in files)    
  17.         {    
  18.             listItems.Add(info);    
  19.         }    
  20.         gvZip.DataSource = listItems;    
  21.         gvZip.DataBind();    
  22.     }    
  23.     protected void Page_Load(object sender, EventArgs e)    
  24.     {    
  25.         BindDataList();    
  26.     }    
  27.     protected void Button1_Click(object sender, EventArgs e)    
  28.     {    
  29.         using (var zip = new ZipFile())    
  30.         {    
  31.             if (FileUpload1.HasFile)    
  32.             {    
  33.                 zip.Password = "123";    
  34.                 zip.Encryption = EncryptionAlgorithm.WinZipAes256;    
  35.                 string filename = Path.GetFileName(FileUpload1.FileName);    
  36.                 FileUpload1.SaveAs(Server.MapPath("~/Upload//") + filename);    
  37.                 string filenameWitoutextension = Path.GetFileNameWithoutExtension(FileUpload1.FileName);    
  38.                 string destdir = Server.MapPath(".") + @"\Zip\" + filenameWitoutextension + ".Zip";    
  39.                 zip.AddDirectory(Server.MapPath(".") + @"\Upload\");    
  40.                 zip.Save(destdir);    
  41.                 string[] files = System.IO.Directory.GetFiles(Server.MapPath("~/Upload//"));    
  42.                 foreach (string f in files)    
  43.                 {    
  44.                     System.IO.File.Delete(f);    
  45.                 }    
  46.                 BindDataList();    
  47.             }    
  48.         }    
  49.     }    
  50.     protected void gvZip_RowCommand(object sender, GridViewCommandEventArgs e)    
  51.     {    
  52.         if (e.CommandName == "Download")    
  53.         {    
  54.             Response.Clear();    
  55.             Response.ContentType = "application/octet-stream";    
  56.             Response.AppendHeader("content-disposition""attachment; filename=" + e.CommandArgument.ToString());    
  57.             Response.TransmitFile(Server.MapPath("~/Zip//" + e.CommandArgument.ToString()));    
  58.             Response.End();    
  59.         }    
  60.     }    
  61. } 
In the code above just check these two lines:
  1. zip.Password = "123";  
  2. zip.Encryption = EncryptionAlgorithm.WinZipAes256;
This is used for password protecting the zip bypassing your password and you can choose your Encryption algorithm.

If you don't want password protection then remove those two lines.

Now build your application. Then choose your file to be zipped.

Click on the "Save and compress" button.

Now it will show your file in the grid with Zip format.

2.png

Now click on the file to download:

3.png

For any modifications or problems please comment.

Thank You.


Similar Articles