Thursday, May 2, 2013

Extract Zip file in Asp.Net using C#




Introduction

In this article, I will explain how to Unzip file in zip folder in ASP.NET. First we download Ionic.Zip.dll from following link or attachment.


The Following is the procedure.

Step 1

Open Visual Studio 2012 and click "File" -> "New" -> "web site...". A window is opened. In this window, click Empty Web Site Application under Visual C#.



Give the name of your application as "Extract_Zip" and then click ok.

Step 2

Now we will need add dll in our website, check below steps

Go to Solution Explorer -> Right click on it and go to Add Reference -> now go to Browse option then select dll.

Now bin folder added to our Web Site and place Ionic.Zip.dll in this folder and add another folder in your application

Right click on your Web Site -> Select New folder and give name as ZipFile. Our Web Site will be like this




Complete Program
ExtractZip.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ExtractZip.aspx.cs" Inherits="ExtractZip" %>
 
<!DOCTYPE html>
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div style="font-weight: bold; color: #0000FF; font-size: large">   
        Extract Zip in Asp.net<br />
        <br />  
    </div>
        <div>
            <asp:FileUpload ID="FileUpload1" runat="server" />
            <br />
            <br />
            <asp:Button ID="bttnupload" runat="server" Font-Bold="True" Text="Upload" OnClick="bttnupload_Click" />
&nbsp;&nbsp;&nbsp;
            <asp:Button ID="bttnzip" runat="server" Font-Bold="True" Text="Create Zip" OnClick="bttnzip_Click" />
&nbsp;&nbsp;
            <asp:Button ID="bttnextract" runat="server" Font-Bold="True" Text="Extract Zip" OnClick="bttnextract_Click" />
        </div>
        <br />
        <asp:Label ID="lbltxt" runat="server" Font-Bold="True" ForeColor="#339933"></asp:Label>
        <br />
        <asp:GridView ID="gdview" runat="server" AutoGenerateColumns="false">
            <Columns>
                <asp:BoundField DataField="Filename" HeaderText="FileName" />
                <asp:BoundField DataField="CompressedSize" HeaderText="Zip Size" />
                <asp:BoundField DataField="UnCompressedSize" HeaderText="UnZip Size" />
            </Columns>
            <HeaderStyle BackColor="SkyBlue" ForeColor="White" Font-Bold="true" />
        </asp:GridView>
    </form>
</body>
</html>
  
ExtractZip.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using Ionic.Zip;
 
public partial class ExtractZip : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
 
    }
    protected void bttnupload_Click(object sender, EventArgs e)
    {
        if (FileUpload1.HasFile)
        {
            string fname = Path.GetFileName(FileUpload1.PostedFile.FileName);
            string path = Server.MapPath("~/ZipFile/" + fname);
            FileUpload1.SaveAs(path);
            lbltxt.Text = "File Uploaded Successfully";
        }
    }
 
    protected void bttnzip_Click(object sender, System.EventArgs e)
    {
        string path = Server.MapPath("~/ZipFile/");
        string[] filenames = Directory.GetFiles(path);
        using (ZipFile zip = new ZipFile())
        {
            zip.AddFiles(filenames, "files");
            zip.Save(Server.MapPath("~/ZipFile.zip"));
            lbltxt.Text = "Zip Created Successfully";
        }
    }
 
    protected void bttnextract_Click(object sender, System.EventArgs e)
    {
        string path = Server.MapPath("~/ZipFile.zip");
        using (ZipFile zip = ZipFile.Read(path))
        {
            zip.ExtractAll(Server.MapPath("~/ExtractZipFile"), ExtractExistingFileAction.DoNotOverwrite);
            gdview.DataSource = zip;
            gdview.DataBind();
            lbltxt.Text = "All File Extracted Successfully";
        }
    }
}
Output 1


Click on "Browse File" and select any file from any drive.


Output 2
Click on the "Upload" Button.


Output 3
Now click on the "Create Zip" button.


Output 4
At Last click on "Extract Zip" button

Output 5
Now again following above steps


Now see that the Solution Explorer contains a Zip folder and Extract folder. Some files have created in both folder.


  

No comments:

Post a Comment

Kashmir 370 and 35A : The Wound of india

क्या है जम्मू-कश्मीर में लागू धारा 370,35A पूर्ण विवरण Know about 370 act in Jammu एक बार फिर से राजनीति गलियारे में धारा 370,35A को ल...