Because the project needs to package the file, at the colleague's suggestion to write a packing function with Icsharpcode. Icsharpcode's introduction will not say. Please go to the official website http://www.icsharpcode.net/.
First quote ICSharpCode.SharpZipLib.dll, not downloaded here: Http://files.cnblogs.com/KenBlove/ICSharpCode.SharpZipLib.rar
Code implementation of multiple files, custom files, whole directory packaging and other functions. All right. The code:
Compress Packaging Code
<summary>///zips the specified zip path. </summary>///<param name= "Strzippath" >the zip path.</param>///<param name= "strziptopd Irectorypath ">the zip top directory path.</param>///<param name=" intziplevel ">the zip Level.</para m>///<param name= "strpassword" >the password.</param>,///<param name= "FilesOrDirectoriesPaths" >the files or directories paths.</param>///<returns></returns> private bool Zip (string Strz
Ippath, string strziptopdirectorypath, int intziplevel, String strpassword, string[] filesordirectoriespaths) {
try {list<string> Allfilespath = new list<string> (); if (Filesordirectoriespaths.length > 0)//Get all Files path {for (int i = 0; i < file Sordirectoriespaths.length; i++) {if (file.exists) (FilesordirectoriespAths[i])) {Allfilespath.add (filesordirectoriespaths[i]);
else if (directory.exists (Filesordirectoriespaths[i])) {
Getdirectoryfiles (Filesordirectoriespaths[i], allfilespath); }} if (Allfilespath.count > 0) {zipoutputstream
Zipoutputstream = new Zipoutputstream (File.create (Strzippath));
Zipoutputstream.setlevel (Intziplevel);
Zipoutputstream.password = strpassword; for (int i = 0; i < Allfilespath.count i++) {string strfile = Allfilespath[i].
ToString ();
try {if (strfile.substring (strfile.length-1) = = "\")//folder {String strFileName = Strfile.replace (strziptoPdirectorypath, ""); if (Strfilename.startswith ("\")) {strFileName = strFileName.
Substring (1);
} zipentry Entry = new ZipEntry (strFileName); Entry.
DateTime = DateTime.Now;
Zipoutputstream.putnextentry (entry); else//file {FileStream fs = File.openread (
strfile); byte[] buffer = new BYTE[FS.
Length]; Fs. Read (buffer, 0, buffer.)
Length);
String strFileName = Strfile.replace (Strziptopdirectorypath, ""); if (Strfilename.startswith ("\")) {strFileName = strFileName.
Substring (1);
} ZipEntry entry = new ZipEntry (strFileName); Entry.
DateTime = DateTime.Now;
Zipoutputstream.putnextentry (entry); Zipoutputstream.write (buffer, 0, buffer.
Length); Fs.
Close (); Fs.
Dispose ();
The catch {continue;
} zipoutputstream.finish ();
Zipoutputstream.close ();
return true;
else {return false;
catch {return false;
}///<summary>///Gets the directory files. </summary>///<param name= "Strparentdirectorypath" >the parent directory path.</param>///& Lt;param name= "Allfilespath"; All Files path.</param> private void Getdirectoryfiles (String strparentdirectorypath, list<string> AllFile
spath) {string[] files = directory.getfiles (Strparentdirectorypath); for (int i = 0; i < files. Length;
i++) {Allfilespath.add (files[i]);
} string[] Directorys = directory.getdirectories (Strparentdirectorypath); for (int i = 0; i < Directorys. Length;
i++) {getdirectoryfiles (directorys[i], allfilespath); } if (Files. Length = = 0 && directorys.
Length = = 0)//empty folder {Allfilespath.add (Strparentdirectorypath); }
}