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 {///// The absolute path of the file to be compressed //Private List
absolutepaths {set; get;} public string ErrorMsg {set; get;} Public Ziphelper () {errormsg = ""; absolutepaths = new List
(); } ///
///Add compressed files or Folders//////
Absolute path to a file or folderpublic void AddFile (string _fileabsolutepath) {Absolutepaths.add (_fileabsolutepath); } ///
///Compressed files or Folders//////
Compressed file storage path, such as C:\\windows\abc.zip///
Compression level 0~9, the larger the number, the higher the compression rate, the default is 5///
public bool Compressionzip (string _depositpath,int _level=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.openread (path); byte[] BTS = new BYTE[FS. Length]; Fs. Read (BTS, 0, BTS. Length); ZipEntry ze = new ZipEntry (new FileInfo (path). Name); Comstream.putnextentry (Ze); Provide a container for compressed file streams 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 is {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}}}///
/// unzip /////
Compressed file path///
Unzip the path///
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 the 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 = f Loderpath + "\" + strs[j]; } Floderpath = Floderpath + "\ \" + strs[i]; Directory.CreateDirectory (Floderpath); }} fs = new FileStream (_floderpath + "\ \" + Ze. Name, FileMode.OpenOrCreate, FileAccess.Write);//create File//loop to read file to file stream Whil E (True) {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; } } }