How to Create ZIP file from Server Folder in Asp.Net



In this Example we will Create Zip folder from Server for that we have to download Iconic. Zip File and using this we will directly create zip file from server.

Download Iconic.zip file

Now on your Webpage create button and Code for Crating Zip File from Server and save in Desktop.

<div style="font: 13px Verdana;">
 <p>
   <input id="Button1" type="button" onserverclick="nowpackit" value="Zip It" runat="server" /></p>
 <p>
 <label id="lblMsg" runat="server"></label>
 </p>
</div>

Now Generating Button Click Event and Code for creating Zip File.

Protected void nowpackit(object sender, EventArgs e)
{
  // ZIP ALL FILES IN THE FOLDER.
  using (var zip = new ZipFile())
  {
    // CREATE A FILE USING A STRING.
    // THE FILE WILL BE STORED INSIDE THE ZIP FILE.
    zip.AddEntry("Content.txt", "This Zip has 2 DOC files");
    // ZIP THE FOLDER WITH THE FILES IN IT.
    zip.AddFiles(Directory.GetFiles(Server.MapPath("~/doc-files/")), "packed");
    zip.Save("C:/Users/umesh.adroja/Desktop/ZIPFile.zip");//SAVE ZIP FILE.
    lblMsg.InnerHtml = "Folder Zipped.";
  }
}

Don’t forget to add name space Iconic.Zip in you Code behind.

using Ionic.Zip;

Now run you Webpage click on button to Generate ZIP File and Open your ZIP file from your desktop.

Related Posts

Previous
Next Post »

Thanks for comments.....