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

Make Zip and Unzip Files in using C-Sharp

Viewed:  4768 
Posted On:  12/05/2013 12:45:22 
How we can Make Zip and Unzip Files in 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 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.


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.

Image 2.

You can see zip file created...


Image 3.

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


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" "">
<html xmlns="">
<head runat="server">
    <title>Make Zip and UnZip folder in using c#</title>
    <form id="form1" runat="server">
    <table width="45%" cellpadding="4" cellspacing="4" align="center" border="1">
            <td align="center" style="background: #F5F5F5;">
                Utility to Make Zip and UnZip(Extract) files.
            <td align="center">
                <asp:Button ID="btnZip" Text="Create Zip Files" runat="server" OnClick="btnZip_Click" />
            <td align="center">
                <asp:Button ID="btnUnZip" Text="Extract Zip Files" runat="server" OnClick="btnunZip_Click" /><br />
    <div align="center">
        <asp:Label ID="lbltxt" runat="server" Font-Bold="true" ForeColor="Red" /></div>

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");
            lbltxt.Text = "ZIP File Created Successfully.";
    protected void btnunZip_Click(object sender, EventArgs e)
        string path = Server.MapPath("~/");
        using (ZipFile zip = ZipFile.Read(path))
            zip.ExtractAll(Server.MapPath("~/MyExtractFiles"), ExtractExistingFileAction.DoNotOverwrite);          
            lbltxt.Text = "Files Extracted Successfully.";

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