Create Zip file using .net

No.of Views1846
Bookmarked0 times
Downloads 
Votes0
By  Kirti.M.Darji   On  15 Feb 2010 22:02:12
Tag : CSharp , Miscellaneous
Create Zip file using .net
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

Introduction

This article explain you about how to create zip file programmatically using .net

Technologies
.NET Framework 2.0 or later

Language
C#

Prerequisite
Visual Studio 2005 and Later

Implementation


1.Right click on project and click on Add References.

2.Add vjslib Reference are in Project

Add following namespace

{codecitation class="brush:csharp; gutter: true;" width="650px"}

using System.IO;
using java.io;
using java.util.zip;

{/codecitation}

Step-1

Take one buttons in Aspx Page like,Copy-Past following code in aspx page

{codecitation class="brush: html; gutter: true;" width="650px"}

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="CreateZip.aspx.cs" Inherits="CreateZip" %>
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>Untitled Pagetitle>
head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="btnZip" runat="server" Text="Zip Folder"
onclick="btnZip_Click" />
<br />
<br />
<asp:Label ID="lbReport" runat="server">asp:Label>
div>
form>
body>
html>

{/codecitation}


Step-2

Copy-past following two method in code file (aspx.cs) of Page


{codecitation class="brush: xml; gutter: true;" width="650px"}


protected void btnZip_Click(object sender, EventArgs e)
{
StringBuilder sb = new StringBuilder();
string ZFileName = String.Format(@"C:\zipfolder\{0}.zip", DateTime.Now.ToString("yyyyMMdd"));
string strDirectory = @"C:\Kirti";
try
{
sb.Append(String.Format("Directory To Zip \n", strDirectory));
sb.Append(String.Format("Zip file: {0}\n", ZFileName));
string[] allFiles = Directory.GetFiles(strDirectory, "*.*", SearchOption.AllDirectories);
if (System.IO.File.Exists(ZFileName))
{
System.IO.File.Delete(ZFileName);
sb.Append(String.Format("Deleted Zip file: \n", ZFileName));
}
FileOutputStream Flostr = new FileOutputStream(ZFileName);
ZipOutputStream Zpoutstr = new ZipOutputStream(Flostr);
Zpoutstr.setLevel(9);
for (int i = 0; i < allFiles.Length; i++)
{
string sourceFile = allFiles[i];
FileInputStream Finstr = new FileInputStream(sourceFile);
ZipEntry ze = new ZipEntry(sourceFile.Replace(strDirectory + @"\", ""));
Zpoutstr.putNextEntry(ze);
sbyte[] buffer = new sbyte[1024];
int len;
while ((len = Finstr.read(buffer)) >= 0)
{
Zpoutstr.write(buffer, 0, len);
}
Finstr.close();
}
Zpoutstr.closeEntry();
Zpoutstr.close();
Flostr.close();
sb.Append(String.Format("Folder {0} Zipped successfuly to File {1}.", strDirectory, ZFileName));
}
catch (Exception eX)
{
sb.Append(String.Format("Error zipping folder {0}. Details: {1}. Stack Trace: {2}.", strDirectory, eX.Message, eX.StackTrace));
}
lbReport.Text = sb.ToString();
}

{/codecitation}

Conclusion
We can easily make zip file using .net.

Enjoy!!!!!! Happy Coding.


About the Author


Kirti Darji
P.G.D.C.A, M.SC(Computer Science) 3+ YEAR EXPERIENCE Visual C#.net and VB.net Windows and Web base Application

Expertise :C#.net, VB.Net,ASP.net,SQL Server, MS Access,aJAX, JavaScript,CSS,HTML,XML,N TIRE ARCHITECTURE,oOPS,Web Services,Core Ajax,visual Source safe,IIS

Occupation :SR. Software Developer
Company : MADHUVAN INFO TECH PVT.LTD
Location :AHMADABAD

 
Sign Up to vote for this article
 
About Author
 
Kirti.M.Darji
Occupation-Software Engineer
Company-Maven-Infosoft Pvt.Ltd
Member Type-Expert
Location-Not Provided
Joined date-03 Jun 2009
Home Page-
Blog Page-http://kirtimdarji.blogspot.com/
I am Kirti M. Darji Senior Software Developer Having 3.5 Years Experience In Microsoft .Net(c#) Technologies.
 
 
Other popularSectionarticles
Comments
There is no comments for this articles.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top