Download Multiple Files As Zip File Archive In Asp.Net

This Example Explains How To Download Multiple Files As Zip File Archive From Server In Asp.Net.

I have placed one CheckBoxList on asp.net page to Display Files From Server And One button to Download Zip File Archives.

I have used DotNetZip Library to Create Zip File from selected file(s), Add Ionic.Zip.dll in Bin folder in application root.
Download Multiple files As Zip FIle In Asp.Net

HTML SOURCE OF PAGE
   1:  <form id="form1" runat="server">
   2:  <div>
   3:  <asp:CheckBoxList ID="checkBoxList" runat="server"/>
   4:   
   5:  <asp:Button ID="btnDownload" runat="server" 
   6:              onclick="btnDownload_Click" 
   7:              Text="Download Files" />
   8:  </div>
   9:  </form>

C# CODE
using System;
using System.Web.UI.WebControls;
using System.IO;
using Ionic.Zip;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DirectoryInfo directory = new DirectoryInfo(Server.MapPath("~/csharpdotnetfreak.blogspot.com"));
            FileInfo[] filesInFolder = directory.GetFiles("*.*", SearchOption.AllDirectories);
            foreach (FileInfo fileInfo in filesInFolder)
            {
                checkBoxList.Items.Add(fileInfo.Name);
            }
        }

    }
    protected void btnDownload_Click(object sender, EventArgs e)
    {
        ZipFile multipleFiles = new ZipFile();
        Response.AddHeader("Content-Disposition", "attachment; filename=DownloadedFile.zip");
        Response.ContentType = "application/zip";
        foreach (ListItem fileName in checkBoxList.Items)
        {
            if (fileName.Selected)
            {
                string filePath = Server.MapPath("~/csharpdotnetfreak.blogspot.com" + fileName.Value);
                multipleFiles.AddFile(filePath,string.Empty);
            }
        }
        multipleFiles.Save(Response.OutputStream);
    }
}

VB.NET
Imports System.Web.UI.WebControls
Imports System.IO
Imports Ionic.Zip

Public Partial Class _Default
 Inherits System.Web.UI.Page
 Protected Sub Page_Load(sender As Object, e As EventArgs)
  If Not IsPostBack Then
   Dim directory As New DirectoryInfo(Server.MapPath("~/csharpdotnetfreak.blogspot.com"))
   Dim filesInFolder As FileInfo() = directory.GetFiles("*.*", SearchOption.AllDirectories)
   For Each fileInfo As FileInfo In filesInFolder
    checkBoxList.Items.Add(fileInfo.Name)
   Next
  End If

 End Sub
 Protected Sub btnDownload_Click(sender As Object, e As EventArgs)
  Dim multipleFiles As New ZipFile()
  Response.AddHeader("Content-Disposition", "attachment; filename=DownloadedFile.zip")
  Response.ContentType = "application/zip"
  For Each fileName As ListItem In checkBoxList.Items
   If fileName.Selected Then
    Dim filePath As String = Server.MapPath("~/csharpdotnetfreak.blogspot.com" & fileName.Value)
    multipleFiles.AddFile(filePath, String.Empty)
   End If
  Next
  multipleFiles.Save(Response.OutputStream)
 End Sub
End Class


Download Sample Code


If you like this post than join us or share

Find More Articles