Email: Password:       Forgot Password 
    .netCodeSG
A Saarsha Group Online Community for dot net codes group like C#, Asp.NET, VB.NET, Sharepoint, JavaScript, JQuery, Ajax, SQL, WCF, WPF.
 
TECHNOLOGIES:
 

Make Zip and Unzip Files in Asp.net using C-Sharp

Article:
Viewed:  3564 
Posted On:  12/05/2013 12:45:22 
How we can Make Zip and Unzip Files in Asp.net using C# 

 In this article I am going to explain how we can make a zip of files and how we can make unzip(extract) files in asp.net. To achieve this we need to add Ionic.Zip.dll reference from DotnetZIP Library.

Now let me explain the working of my application..

Below image shows my add Ionic.Zip.dll reference.

1. MyUploadedFile -> We will make zip of all files of this folder and save on root.

2. MyExtractFile -> We will  save all extracted file from a zip file.

1.png

Image 1.

Below is my Application working.. On Click of Create Zip Files I am making a zip file. And on Click of Extract Zip Files, I am extracting all zip files.

2.png
Image 2.

You can see zip file created...

3.png

Image 3.

On Click of Extract Zip Files you can see all extracted files inside MyExtractFiles folder...

4.png

Image 4.

Now My aspx code is:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Make Zip and UnZip folder in Asp.net using c#</title>
</head>
<body>
    <form id="form1" runat="server">
    <table width="45%" cellpadding="4" cellspacing="4" align="center" border="1">
        <tr>
            <td align="center" style="background: #F5F5F5;">
                Utility to Make Zip and UnZip(Extract) files.
            </td>
        </tr>
        <tr>
            <td align="center">
                <asp:Button ID="btnZip" Text="Create Zip Files" runat="server" OnClick="btnZip_Click" />
            </td>
        </tr>
        <tr>
            <td align="center">
                <asp:Button ID="btnUnZip" Text="Extract Zip Files" runat="server" OnClick="btnunZip_Click" /><br />
            </td>
        </tr>
    </table>
    <div align="center">
        <asp:Label ID="lbltxt" runat="server" Font-Bold="true" ForeColor="Red" /></div>
    </form>
</body>
</html>
 

My aspx.cs code is:

using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System;
using System.IO;
using Ionic.Zip;


public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
 
    }
 
    protected void btnZip_Click(object sender, EventArgs e)
    {
        string path = Server.MapPath("~/MyUploadedFiles/");
        string[] filenames = Directory.GetFiles(path);
        using (ZipFile zip = new ZipFile())
        {
            zip.AddFiles(filenames, "files");
            zip.Save(Server.MapPath("~/MyUploadedFiles.zip"));
            lbltxt.Text = "ZIP File Created Successfully.";
        }
    }
 
    protected void btnunZip_Click(object sender, EventArgs e)
    {
        string path = Server.MapPath("~/MyUploadedFiles.zip");
        using (ZipFile zip = ZipFile.Read(path))
        {
            zip.ExtractAll(Server.MapPath("~/MyExtractFiles"), ExtractExistingFileAction.DoNotOverwrite);          
            lbltxt.Text = "Files Extracted Successfully.";
        }
    }
}

  Comment:
         HOME   |   Submit Article   |   Contact Us   |   About Us   |   Terms & Condition   |   Advertise With us