Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

How to create Zip file in ASP.NET

Author: Nikki Smith
Download Source Code : 1333_CreateZipFile.zip

In this article, I will show you how you can create a zip file and download it in ASP.NET.

 

Zip file is one of the popular file formats to compress and archive files. It is often used to reduce the file size and to group multiple files in a single file.
 
You can use any librarie or dll available to create Zip file in ASP.NET but I am using DotNetZip library in this article. DotNetZip library is free, open source and easy to use library to zip, unzip and manipulate zip files in your ASP.NET application.

First I will display images in a CheckBoxList control and then I will download selected images in a zip file. You can download source code.

 

  1. Create a new Empty Web Site in Visual Studio 2010 either in Visual Basic or Visual C#.
  2. Add a Web Form in the Web Site. No Need to change name of the Page
  3. Add a CheckBoxList control and a Button control to Web Site

    <asp:CheckBoxList ID="CheckBoxList1" runat="server">
    </asp:CheckBoxList>
    <br />
    <asp:Button ID="Button1" runat="server" Text="Download all files in a Zip file"
        onclick="Button1_Click" />
     
  4. Now add reference for “Ionic.Zip.dll”. You need to download DotNetZip library first and then add reference in your Web Site. You will find Ionic.Zip.dll file in tools subfolder of DotNetZip directory.
     
  5. Add a Folder in the Web site and rename the folder as "Images". Add some images to the folder.
     
  6. You need to use following namespaces in user code file.

    Visual Basic
     
    Imports System.IO
    Imports Ionic.Zip
     
    Visual C#
     
    using System.IO;
    using Ionic.Zip;
     
  7. Write below code in Page Load event

    I have created a DirectoryInfo object by providing path of the folder. Then I have created a FileInfo object and get all files by calling GetFiles() method. I have used a “For Each” loop to get file name of each file and add item to CheckBoxList.
     
    Visual Basic
     
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim dir As New DirectoryInfo(Server.MapPath("~/Images"))
     
        Dim files As FileInfo() = dir.GetFiles()
     
        For Each file As FileInfo In files
            Dim fileName As String = Path.GetFileName(file.ToString())
            Dim item As New ListItem(fileName)
            CheckBoxList1.Items.Add(item)
        Next
    End Sub
     
    Visual C#
     
    protected void Page_Load(object sender, EventArgs e)
    {
        DirectoryInfo dir = new DirectoryInfo(Server.MapPath("~/Images"));
     
        FileInfo[] files = dir.GetFiles();
     
        foreach (FileInfo file in files)
        {
            string fileName = Path.GetFileName(file.ToString());
            ListItem item = new ListItem(fileName);
            CheckBoxList1.Items.Add(item);
        }
    }
     
  8. Now write below code in button click event to create zip file and download it.

    I have created an object of ZipFile class. Then I have set the content type of zip file and call the Addheader() method by providing the file name of zip file. I have used a “For Each” loop to get the selected image and add this image to zip file. Then I have called the Save() method of the ZipFile object by providing output stream.
     
    Visual Basic
     
    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Using zipFile As New ZipFile()
            Response.Clear()
            Response.ContentType = "application/zip"
            Response.AddHeader("content-disposition", "filename=Images.zip")
     
            For Each item As ListItem In CheckBoxList1.Items
                If item.Selected Then
                    zipFile.AddFile(item.Value)
                End If
            Next
            zipFile.Save(Response.OutputStream)
        End Using
    End Sub
     
    Visual C#
     
    protected void Button1_Click(object sender, EventArgs e)
    {
        using (ZipFile zipFile = new ZipFile())
        {
            Response.Clear();
            Response.ContentType = "application/zip";
            Response.AddHeader("content-disposition", "filename=Images.zip");
     
            foreach (ListItem item in CheckBoxList1.Items)
            {
                if (item.Selected)
                {
                    zipFile.AddFile(item.Value);
                }
            }
            zipFile.Save(Response.OutputStream);
        }
    }
     
  9. Now you can see web site in your browser

 

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< How to bind objects to GridView control in ASP.NET

Disclaimer - Privacy
© 2002-2017 DevASP.net