Hi tanweeruddinb...,
Use ZipFile AddDirectory method to zip complete folder.
Refer below sample.
HTML
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" EmptyDataText="No files available">
<Columns>
<asp:BoundField DataField="Text" HeaderText="File Name" />
</Columns>
</asp:GridView>
<br />
<asp:Button ID="btnDownload" runat="server" Text="Download" OnClick="DownloadFiles" />
Namespaces
C#
using System.IO;
using Ionic.Zip;
using System.Collections.Generic;
VB.Net
Imports System.IO
Imports Ionic.Zip
Imports System.Collections.Generic
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string[] filePaths = Directory.GetFiles(Server.MapPath("~/Upload/Files/"));
List<ListItem> files = new List<ListItem>();
foreach (string filePath in filePaths)
{
files.Add(new ListItem(Path.GetFileName(filePath), filePath));
}
GridView1.DataSource = files;
GridView1.DataBind();
}
}
protected void DownloadFiles(object sender, EventArgs e)
{
using (ZipFile zip = new ZipFile())
{
zip.AlternateEncodingUsage = ZipOption.AsNecessary;
zip.AddDirectory(Server.MapPath("~/Upload/"));
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 Page_Load(sender As Object, e As EventArgs) Handles Me.Load
If Not IsPostBack Then
Dim filePaths As String() = Directory.GetFiles(Server.MapPath("~/Upload/Files/"))
Dim files As New List(Of ListItem)()
For Each filePath As String In filePaths
files.Add(New ListItem(Path.GetFileName(filePath), filePath))
Next
GridView1.DataSource = files
GridView1.DataBind()
End If
End Sub
Protected Sub DownloadFiles(sender As Object, e As EventArgs)
Using zip As New ZipFile()
zip.AlternateEncodingUsage = ZipOption.AsNecessary
zip.AddDirectory(Server.MapPath("~/Upload/"))
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
Screenshot