In the project is often used to extract the public class, uncompressed files occupy a small space, can also be encrypted, often can be used to upload attachments to the client, packaging output main program, etc., the benefits of which are not much said, recently started the project many times to understand the compression method, Now more popular is icsharpcode, stable, efficient, is a good decompression package class. Through the Internet and personal collation, now the class to share out, as a resource to share to everyone, so you can not be immersed in the bitter brain in the internet hard to find, nonsense not much to say, on the code:
Using System;
Using System.Collections.Generic;
Using System.IO;
Using System.Security.Cryptography;
Using ICSharpCode.SharpZipLib.Core;
Using ICSharpCode.SharpZipLib.Zip;
Namespace Helper {public class Utily {///<summary>///fast compression///</summary> <param name= "Filespath" > Folder paths to be compressed </param>///<param name= "Zipfilepath" > Output path </PA ram>///<param name= "pwd" > password, can not write </param>///<param name= "FileFilter" > Filter conditions </param >///<param name= "createemptydirectories" > whether to compress empty folders </param>///<param name= "Progressfun "> Process </param>///<param name=" seconds "> Triggered seconds </param>///<param name=" Completefu N "> Completion events </param> public static void Createzipfile (String filespath, String Zipfilepath, string pwd, string FileFilter, bool createemptydirectories, Progresshandler Progressfun, double seconds, ComPletedfilehandler completefun) {fastzipevents events = new Fastzipevents (); if (progressfun!= null) {events.
Progress = Progressfun; Events.
Progressinterval = timespan.fromseconds (seconds);
} if (Completefun!= null) {events.completedfile = Completefun;
} fastzip Zip = new Fastzip (events); Zip.
Createemptydirectories = createemptydirectories; if (!string. IsNullOrEmpty (pwd)) zip.
Password = pwd; Zip.
USEZIP64 = Usezip64.on; Zip.
Restoreattributesonextract = true; Zip.
Restoredatetimeonextract = true; Zip.
Createzip (Zipfilepath, Filespath, True, FileFilter); }///<summary>///quick decompression///</summary>///<param name= "Zipfilepath" & GT; Compressed file path </param>///<param name= "Extractpath" > DecompressionPath </param>///<param name= "pwd" > Compressed password </param>///<param name= "Progressfun" > Process < /param>///<param name= "seconds" > Trigger time </param> public static void Extractzipfile (String zip FilePath, String Extractpath, string pwd, Progresshandler progressfun, double seconds) {fastzipevents
Events = new Fastzipevents (); if (progressfun!= null) {events.
Progress = Progressfun; Events.
Progressinterval = timespan.fromseconds (seconds);
} fastzip Zip = new Fastzip (events); Zip.
Createemptydirectories = true; if (!string. IsNullOrEmpty (pwd)) zip.
Password = pwd; Zip.
USEZIP64 = Usezip64.on; Zip.
Restoreattributesonextract = true; Zip.
Restoredatetimeonextract = true; Zip. Extractzip (Zipfilepath, Extractpath, FastZip.Overwrite.Always, NULL, "", "", true);
}///<summary>///quick decompression///</summary>///<param name= "ZipF Ilepath "> Compressed file path </param>///<param name=" Extractpath "> Decompression path </param>///<param name = "pwd" > Password </param>///<param name= "Progressfun" > Process </param>///<param name= "second S "> Trigger time </param>///<param name=" Completefun "> Functions performed in the compression process </param> public static void E Xtractzipfile (String Zipfilepath, String Extractpath, string pwd, Progresshandler progressfun, double seconds,
Completedfilehandler completefun) {fastzipevents events = new Fastzipevents (); if (progressfun!= null) {events.
Progress = Progressfun; Events.
Progressinterval = timespan.fromseconds (seconds);
} if (Completefun!= null) {events.completedfile = Completefun; }
Fastzip zip = new Fastzip (events); Zip.
Createemptydirectories = true; if (!string. IsNullOrEmpty (pwd)) zip.
Password = pwd; Zip.
USEZIP64 = Usezip64.on; Zip.
Restoreattributesonextract = true; Zip.
Restoredatetimeonextract = true; Zip.
Extractzip (Zipfilepath, Extractpath, FastZip.Overwrite.Always, NULL, "", "", true); ///<summary>///Gets the total size of the original file in the compressed package///</summary>///<param name= "Filen Ame "></param>///<param name=" FileFilter "></param>///<param name=" directoryfilt Er ' ></param>///<returns></returns> public static long Getzipfilesize (string Filenam
E, String filefilter, String directoryfilter) {Long b = 0; using (ZipFile zipfile = new ZipFile (fileName)) {Pathfilter Localfilefilter = new PatHfilter (FileFilter);
Pathfilter localdirfilter = new Pathfilter (directoryfilter);
if (Zipfile.count = = 0) {return 0; for (int i = 0; i < Zipfile.count ++i) {ZipEntry e = zipfile[i]
;
if (e.isfile) {String path = Path.getdirectoryname (e.name); if (Localdirfilter.ismatch (path)) {if (LOCALFILEFILTER.ISMATC
H (Path.getfilename (e.name))) {b + = e.size;
}}} return b; ///<summary>///obtained MD5 checksum///</summary>///<param name= "Filepat H "></param>///<returns></returns> public static string GetMD5 (String filepath) {string returnstr = "";
FileStream fs = new FileStream (filepath, FileMode.Open, FileAccess.Read);
MD5CryptoServiceProvider MD5 = new MD5CryptoServiceProvider (); Byte[] Md5byte = MD5.
ComputeHash (FS);
int I, J;
foreach (Byte b in md5byte) {i = Convert.ToInt32 (b);
j = i >> 4;
Returnstr + + convert.tostring (j, 16);
j = ((I << 4) & 0X00FF) >> 4;
Returnstr + + convert.tostring (j, 16); } fs.
Dispose ();
return returnstr; ///<summary>///uncompress files for specific file names///</summary>///<param name= "path" & gt; file path </param>///<param name= "addres" > Decompression path </param>///view this column more highlights: HTTP://WWW.BIANCENG.CNHT Tp://www.bianceng.cn/programming/net////<param name= "zipfilename" > file name </param>///<param name= "pwd" > Uncompressed package Password </
param> public static void Ziptofile (string path, string addres, String zipfilename, string pwd) {
Zipinputstream ZIPstream = new Zipinputstream (System.IO.File.OpenRead (path)); if (!string.
IsNullOrEmpty (pwd)) Zipstream.password = pwd;
ZipEntry Fileentry; while ((Fileentry = Zipstream.getnextentry ())!= null) {string filename = Path.getfilename (fi
Leentry.name);
if (filename = = zipfilename) {filename = addres + "\" + filename;
FileStream StreamWriter = System.IO.File.Create (filename);
int size = (int) fileentry.size;
byte[] buffer = new Byte[size];
Size = zipstream.read (buffer, 0, size); Streamwriter.wriTe (buffer, 0, size);
StreamWriter.Close ();
} zipstream.close (); }
}
}
This class can meet the basic methods of decompression, but now more popular should be 7z compression, this is also in the study, the above code if there is not correct place, please correct me. As for the Icsharpcode DLL file, the Internet can download a lot of places, I will not give out the download address.
Warm reminder: When referencing Icsharpcode, you also reference Icsharpcode on the class library or application that invokes the method, or you can generate an error.