Hi suhaas121,
For adding binary data to zip file you need to use AddEntry method.
Check the below code.
C#
protected void Zip(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["connString"].ConnectionString);
using (ZipFile zip = new ZipFile())
{
zip.AlternateEncodingUsage = ZipOption.AsNecessary;
SqlCommand cmd = new SqlCommand();
conn.Open();
cmd.Connection = conn;
cmd.CommandType = CommandType.Text;
cmd.CommandText = "select tbn.Rawdata_img,tbn.Rawdata_tbl,tbn.Rawfilename_img,tbn.Rawfilename_tbl from tblBookNodes_IPC tbn inner join tblModule tbm on tbn.iModuleId=tbm.iModuleId where tbm.vcModuleTitle=@title";
cmd.Parameters.AddWithValue("@title", txtDownload.Text.Trim());
using (SqlDataReader dr = cmd.ExecuteReader())
{
while (dr.Read())
{
byte[] bytes = (byte[])dr["Rawdata_img"];
byte[] bytes_img = (byte[])dr["Rawdata_tbl"];
string filename = dr["Rawfilename_img"].ToString();
string filename_ipc = dr["Rawfilename_tbl"].ToString();
zip.AddEntry(filename, bytes);
zip.AddEntry(filename_ipc, bytes_img);
}
}
conn.Close();
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 Zip(ByVal sender As Object, ByVal e As EventArgs)
Dim conn As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("connString").ConnectionString)
Using zip As ZipFile = New ZipFile()
zip.AlternateEncodingUsage = ZipOption.AsNecessary
Dim cmd As SqlCommand = New SqlCommand()
conn.Open()
cmd.Connection = conn
cmd.CommandType = CommandType.Text
cmd.CommandText = "select tbn.Rawdata_img,tbn.Rawdata_tbl,tbn.Rawfilename_img,tbn.Rawfilename_tbl from tblBookNodes_IPC tbn inner join tblModule tbm on tbn.iModuleId=tbm.iModuleId where tbm.vcModuleTitle=@title"
cmd.Parameters.AddWithValue("@title", txtDownload.Text.Trim())
Using dr As SqlDataReader = cmd.ExecuteReader()
While dr.Read()
Dim bytes As Byte() = CType(dr("Rawdata_img"), Byte())
Dim bytes_img As Byte() = CType(dr("Rawdata_tbl"), Byte())
Dim filename As String = dr("Rawfilename_img").ToString()
Dim filename_ipc As String = dr("Rawfilename_tbl").ToString()
zip.AddEntry(filename, bytes)
zip.AddEntry(filename_ipc, bytes_img)
End While
End Using
conn.Close()
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