Hi itsme,
Check this example. Now please take its reference and correct your code.
HTML
<asp:TextBox runat="server" ID="txtName" />
<asp:Button Text="Download" runat="server" OnClick="OnDownload" />
Namespaces
C#
using System.IO;
using Ionic.Zip;
VB.Net
Imports System.IO
Imports Ionic.Zip
Code
C#
protected void OnDownload(object sender, EventArgs e)
{
string folderPath = string.Format(Server.MapPath("~/{0}/"), txtName.Text);
// Get files from database based on organisation and document name.
string[] filePaths = Directory.GetFiles(folderPath);
using (ZipFile zip = new ZipFile())
{
zip.AlternateEncodingUsage = ZipOption.AsNecessary;
zip.AddDirectoryByName("Files");
foreach (string file in filePaths)
{
zip.AddFile(file, "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 OnDownload(ByVal sender As Object, ByVal e As EventArgs)
Dim folderPath As String = String.Format(Server.MapPath("~/{0}/"), txtName.Text)
' Get files from database based on organisation and document name.
Dim filePaths As String() = Directory.GetFiles(folderPath)
Using zip As ZipFile = New ZipFile()
zip.AlternateEncodingUsage = ZipOption.AsNecessary
zip.AddDirectoryByName("Files")
For Each file As String In filePaths
zip.AddFile(file, "Files")
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
For more details refer below article.