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
Open Visual studion and create website projects
1.Right click on project and click on Add References.
2.Add vjslib Reference are in Project
Add following namespace
using System.IO; using java.io; using java.util.zip;Step-1
Put one button name like btnZip
in aspx Page
Step-2
copy Past Following code in Button click event like
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(); }
Conclusion
This is reference article code for you get some idea about how create zip file. you can make customization on your way like give path dynamic and select zip folder path dynamically etc..
This is reference article code for you get some idea about how create zip file. you can make customization on your way like give path dynamic and select zip folder path dynamically etc..
Happy Coding.
Enjoy!!!!!!
Enjoy!!!!!!