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
|
|
|