Monday 16 September 2013

Extract Unzip Files into Zip Folder or Covert Unzip Files to Zip in Asp.net using C#




Description:-

            Here in this Example I will Explain that How to Extract or Unzip or Convert Files into the Zip Folder in Asp.Net using C#.Net.

We all know that in Internet If you want to Download Multiple Files at a time then we must First have to Extract All Selected File into an Zip Folder then user can Download that Project.

So Implement this Functionality in our Application we must First have to Download the  Ionic.Zip.dll from DotnetZIP Library.once you Download the File then you have to add the dll File into the Bin Folder of your project like as shown in Following Image.






Now Add the SampleFiles folder in your Application for storing the Zip Folder.




Before use this Example you must have to Add the Following Namespace in your Application like 

            using System.IO;
using Ionic.Zip;




Here  Ionic.Zip  is the dll File that we Include in the Bin Folder. And Using the Functionality of the Extract Unzip Files to Zip Folder we must Include this Namespace.

to Download multiple File as Zip File then click here download multiple file as Zip file

To Download the Complete Project then Click on the below Download Image Link
download here!



Default.aspx:-




<%@ 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>
    <title>Convert or Extract or Unzip files from Zip folder in Asp.net using c#</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:FileUpload ID="fileUpload1" runat="server" />
    <asp:Button ID="btnupld" runat="server" Text="Upload selected Files" onclick="btnupld_Click" />   <br />
    <asp:Button ID="btncreatezip" Text="convert to Zip Files" runat="server" onclick="btncreatezip_Click" />
    <asp:Button ID="btnconvertunzip" Text="Convert Zip folder to Files" runat="server" onclick="btnconvertunzip_Click" /><br />
    <asp:Label ID="lblmsg" runat="server" Font-Bold="true" ForeColor="Red" />
    </div>
    <div>
    <asp:GridView ID="grdzip" CellPadding="4" runat="server" AutoGenerateColumns="False" EnableModelValidation="True" ForeColor="#333333" GridLines="None">
        <AlternatingRowStyle BackColor="White" />
    <Columns>
    <asp:BoundField DataField="FileName" HeaderText="FileName" />
    <asp:BoundField DataField="CompressedSize" HeaderText="Zip Size" />
    <asp:BoundField DataField="UnCompressedSize" HeaderText="Unzip Size" />
    </Columns>
        <EditRowStyle BackColor="#2461BF" />
        <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#507CD1" Font-Bold="true" ForeColor="White" />
        <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
        <RowStyle BackColor="#EFF3FB" />
        <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
</asp:GridView>
    </div>
    </form>
</body>
</html>




Default.aspx.cs:-





using System;
using System.IO;
using Ionic.Zip;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    // Add all  files to folder
    protected void btnupld_Click(object sender, EventArgs e)
    {
        if (fileUpload1.HasFile)
        {
            string filename = Path.GetFileName(fileUpload1.PostedFile.FileName);
            string path = Server.MapPath("~/SampleFiles/" + filename);
            fileUpload1.SaveAs(path);
            lblmsg.Text = "File was Successfully Uploaded";
        }
    }
    //convert  all files to zip folder
    protected void btncreatezip_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"));
            lblmsg.Text = "ZIP File was Successfully Created";
        }
    }
    // UnZip all files from zip folder
    protected void btnconvertunzip_Click(object sender, EventArgs e)
    {
        string path = Server.MapPath("~/SampleFiles.zip");
        using (ZipFile zip = ZipFile.Read(path))
        {
            zip.ExtractAll(Server.MapPath("~/ExtractFiles"),ExtractExistingFileAction.DoNotOverwrite);
            grdzip.DataSource = zip;
            grdzip.DataBind();
            lblmsg.Text = "Zip file was Extracted Successfully and it contains the list of files are as follow";
        }
    }
}


 

1 comments:

  1. Really appreciate this Developer work it help me resolved my technical Error i really recommended this blogger blog for you K-Base
    Highly Recommended A++++++++++

    ReplyDelete