Description:
.aspx.CS file
using Ionic.Zip;
Here i will explain how to create ZIP file in asp.net. The ZipFile class makes it easy to
compress directories. With CreateFromDirectory, we specify an input folder and an output file.
compress directories. With CreateFromDirectory, we specify an input folder and an output file.
.aspx file
<asp:FileUpload ID="fileUpload1" runat="server" />
<asp:Button ID="btnUpload" runat="server" Text="Upload Files"onclick="btnUpload_Click" />
<asp:Button ID="btnZip" Text="Create Zip Files" runat="server" onclick="btnZip_Click"/>
<asp:Button ID="btnZip" Text="Create Zip Files" runat="server" onclick="btnZip_Click"/>
.aspx.CS file
- Add below two reference in your .aspx.cs file:
using Ionic.Zip;
protected void btnUpload_Click(object sender, EventArgs e)
{
if(fileUpload1.HasFile)
{
string filename = Path.GetFileName(fileUpload1.PostedFile.FileName);
string path = Server.MapPath("~/SampleFiles/" + filename);
fileUpload1.SaveAs(path);
}
}
// Zip all files from folder
protected void btnZip_Click(object sender, EventArgs e)
{
string path = Server.MapPath("~/SampleFiles/");
string[] filenames = Directory.GetFiles(path);
using (ZipFile zip = new ZipFile())
{
zip.AddFiles(filenames,"files");
zip.Save(Server.MapPath("~/samplefiles.zip"));
}
}
}
No comments:
Post a Comment