This article explains how to create a zip file in ASP.NET using C#. First we download Ionic.Zip.dll from the following link or attachment.

Ionic.Zip.dll

The following is the procedure.

Step 1

Open Visual Studio 2012 and click "File" -> "New" -> "Web site...". A window is opened. In this window, click "Empty Web Site Application" under Visual C#.

application-name.jpg

Give your application the name "Create_Zip" and then click "Ok".

Step 2

Now we will need add a DLL to our website, check the following steps.
Go to Solution Explorer then right-click on the "References" node of the solution then go to "Add Reference" then go to the Browse tab then select the DLL.
Now a bin folder is added to our Web Site and place Ionic.Zip.dll in this folder and add another folder to your application.

Right-click on your Web Site then select "New folder" and give it the name "Zipa". Our Web Site will be like this:

solution-explorer.jpg

Complete Program

CreateZip.aspx

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="CreateZip.aspx.cs" Inherits="CreateZip" %>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head runat="server">
  5. <title></title>
  6. </head>
  7. <body>
  8. <form id="form1" runat="server">
  9. <h3 style="color: #0000FF; font-style: italic"> Create Zip in ASP.NET</h3>
  10. <div style="height: 121px">
  11. <asp:FileUpload ID="FileUpload" runat="server" />
  12. <br />
  13. <br />
  14. <asp:Button ID="bttnupload" runat="server" Text="Upload File" Font-Bold="True" onclick="bttnupload_Click" /
  15. <asp:Button ID="bttnzip" runat="server" Text="Create Zip" Font-Bold="True" onclick="bttnzip_Click" />
  16. <br />
  17. <br />
  18. <asp:Label ID="lbltxt" runat="server" ForeColor="#CC0000"></asp:Label>
  19. </div>
  20. </form>
  21. </body>
  22. </html>

CreateZip.aspx.cs

  1. using System;
  2. using System.Collections.Generic;
  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 CreateZip : System.Web.UI.Page
  10. {
  11. protected void Page_Load(object sender, EventArgs e)
  12. {
  13. }
  14. protected void bttnupload_Click(object sender, EventArgs e)
  15. {
  16. if (FileUpload.HasFile)
  17. {
  18. string filename = Path.GetFileName(FileUpload.PostedFile.FileName);
  19. string pathname = Server.MapPath("~/zipa/" + filename);
  20. FileUpload.SaveAs(pathname);
  21. lbltxt.Text = "File Upload Successfully";
  22. }
  23. }
  24. protected void bttnzip_Click(object sender, EventArgs e)
  25. {
  26. try
  27. {
  28. string pathname = Server.MapPath("~/zipa/");
  29. string[] filename = Directory.GetFiles(pathname);
  30. using (ZipFile zip = new ZipFile())
  31. {
  32. zip.AddFiles(filename, "file");
  33. zip.Save(Server.MapPath("~/zipa.zip"));
  34. lbltxt.Text = "Zip File Created";
  35. }
  36. }
  37. catch (Exception ex)
  38. {
  39. lbltxt.Text = ex.Message;
  40. }
  41. }
  42. }

Output 1

first-image.jpg

Click on "Choose File" and select any file from any drive.

select-file.jpg

Output 2

Click on the "Upload" Button.

click-upload-button.jpg

Output 3

Now click on the "Create Zip" button.

click-on-createzip-button.jpg

Output 4

Now see that the Solution Explorer contains a Zip folder.

final-result.jpg

For more information, download the attached sample application.