Download multiple files as Zip Archive File in ASP.Net using C# and VB.Net

Here Mudassar Ahmed Khan has explained with an example and attached sample code, how to download multiple files from server on single button click through GridView by creating Zip Archive File using DotNetZip Library in ASP.Net using C# and VB.Net

Referencing the DotNetZip Library
You will need to download the DotNetZip Library DLL using the Download Link provided below.
Download DotNetZip Or your will find the DLL in the attached sample at the end of the article.
Once you have the DLL you need to place the Ionic.Zip.Reduced.dll in the BIN Folder.
VB.Net
Imports System.IO
Imports Ionic.Zip
Imports System.Collections.Generic
Downloading Multiple Files selected in GridView as Zip Archive File using DotNetZip
C#
protected void DownloadFiles(object sender, EventArgs e)
{
    using (ZipFile zip = new ZipFile())
    {
        zip.AlternateEncodingUsage = ZipOption.AsNecessary;
        zip.AddDirectoryByName(“Files”);
        foreach (GridViewRow row in GridView1.Rows)
        {
            if ((row.FindControl(“chkSelect”) as CheckBox).Checked)
            {
                string filePath = (row.FindControl(“lblFilePath”) as Label).Text;
                zip.AddFile(filePath, “Files”);
            }
        }
        Response.Clear();
        Response.BufferOutput = false;
        string zipName = String.Format(“Zip_{0}.zip”, DateTime.Now.ToString(“yyyy-MMM-dd-HHmmss”));
        Response.ContentType = “application/zip”;
        Response.AddHeader(“content-disposition”, “attachment; filename=” + zipName);
        zip.Save(Response.OutputStream);
        Response.End();
    }
}
 
VB.Net
Protected Sub DownloadFiles(sender As Object, e As EventArgs)
    Using zip As New ZipFile()
        zip.AlternateEncodingUsage = ZipOption.AsNecessary
        zip.AddDirectoryByName(“Files”)
        For Each row As GridViewRow In GridView1.Rows
            If TryCast(row.FindControl(“chkSelect”), CheckBox).Checked Then
                Dim filePath As String = TryCast(row.FindControl(“lblFilePath”), Label).Text
                zip.AddFile(filePath, “Files”)
            End If
        Next
        Response.Clear()
        Response.BufferOutput = False
        Dim zipName As String = [String].Format(“Zip_{0}.zip”, DateTime.Now.ToString(“yyyy-MMM-dd-HHmmss”))
        Response.ContentType = “application/zip”
        Response.AddHeader(“content-disposition”, “attachment; filename=” + zipName)
        zip.Save(Response.OutputStream)
        Response.[End]()
    End Using
End Sub

http://www.aspsnippets.com/Articles/Download-multiple-files-as-Zip-Archive-File-in-ASPNet-using-C-and-VBNet.aspx

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s