Using system;using system.collections.generic;using system.io;using system.linq;using System.Web;using Icsharpcode.sharpziplib.zip;namespace gzrmis.main.business{public class Ziphelper {//<summary> The absolute path of the file to be compressed///</summary> private list<string> absolutepaths {set; get;} public string ErrorMsg {set; get;} Public Ziphelper () {errormsg = ""; Absolutepaths = new list<string> (); }///<summary>//Add compressed files or Folders///</summary>//<param name= "_fileabsolutepat H "> absolute path to file or folder </param> public void AddFile (string _fileabsolutepath) {Absolutepaths.add (_fileabsolutepath); }///<summary>//Compressed files or Folders///</summary>//<param name= "_depositpath" > Compressed file storage path such as c:\\windows\abc.zip</param>//<param name= "_level" > Compression level 0~9, the higher the number of compression ratio,The default is 5</param>//<returns></returns> public bool Compressionzip (string _depositpath,int _l EVEL=5) {bool result = true; FileStream fs = null; try {zipoutputstream comstream = new Zipoutputstream (File.create (_depositpath)); Comstream.setlevel (_level); Compression level foreach (string path in absolutepaths) {//If it is a directory if (directory.exists (path)) {Zipfloder (path, comstream, path); } else if (file.exists (path))//If the file {fs = File.openre AD (path); byte[] BTS = new BYTE[FS. Length]; Fs. Read (BTS, 0, BTS. Length); ZipEntry ze = new ZipEntry (new FileInfo (path). Name); Comstream.putnextentry (Ze); To compress filesThe stream provides a container comstream.write (BTS, 0, BTS. Length); Write Bytes}} comstream.finish (); End compression Comstream.close (); } catch (Exception ex) {if (fs! = NULL) {fs. Close (); } errormsg = ex. Message; result = false; } return result; }//Compressed folder private void Zipfloder (String _ofloderpath, Zipoutputstream Zos, string _floderpath) { foreach (FileSystemInfo item in New DirectoryInfo (_floderpath). Getfilesysteminfos ()) {if (directory.exists) (item. FullName) {Zipfloder (_ofloderpath, Zos, item. FullName); } else if (File.exists (item. FullName)//If the file {DirectoryInfo ODir = new DirectoryInfo (_ofloderpath); String fullName2 = new FileInfo (item. FullName). FullName; String path = Odir.name + fullname2.substring (ODir.FullName.Length, fullname2.length-odir.fullname.length);//Get relative directory FileStream fs = File.openread (fullName2); byte[] BTS = new BYTE[FS. Length]; Fs. Read (BTS, 0, BTS. Length); ZipEntry ze = new ZipEntry (path); Zos. Putnextentry (Ze); Provides a container zos for compressed file streams. Write (BTS, 0, BTS. Length); Write bytes}}///<summary>//unzip//</summary> <param name= "_depositpath" > Compressed file path </param>//<param name= "_floderpath" > unzipped path </param> <returns></returns> public bool Decompressionzip (string _depositpath, String _floderpath) {bool result = true; FileStream fs = null; try { Zipinputstream Inpstream = new Zipinputstream (File.openread (_depositpath)); ZipEntry ze = inpstream.getnextentry ();//get each file in a compressed file Directory.CreateDirectory (_floderpath);//Create Unzip folder while (ze! = null)//If ze is finished, it is null {if (ze. Isfile)//compressed zipinputstream is stored in the file. The file name with the folder is the folder \ \ filename {string[] STRs = ze. Name.split (' \ \ ');//If the file name contains ' \ \ ' It indicates a folder if (STRs. Length > 1) {//two layer loop for layer one layer create folder for (int i = 0; I < STRs. Length-1; i++) {string floderpath = _floderpath; for (int j = 0; J < i; J + +) {Floderpath = FL Oderpath + "\" + strs[j]; } Floderpath =Floderpath + "\" + strs[i]; Directory.CreateDirectory (Floderpath); }} fs = new FileStream (_floderpath + "\ \" + Ze. Name, FileMode.OpenOrCreate, FileAccess.Write);//create File//loop read file to file stream while (t Rue) {byte[] BTS = new byte[1024]; int i = Inpstream.read (BTS, 0, BTS. Length); if (i > 0) {fs. Write (BTS, 0, I); } else {fs. Flush (); Fs. Close (); Break }}} ze = Inpstream.getnextentry (); } } catch (Exception ex) {if (fs! = NULL) {fs. Close (); } errormsg = ex. Message; result = false; } return result; }}}<a target=_blank href= "http://icsharpcode.github.io/SharpZipLib/" target= "_blank" > ICSharpCode.SharpZipLib.Zip Download </a>
C # Compression Decompression helper class