First reference icsharpcode. sharpziplib. dll, not downloaded here: http://files.cnblogs.com/KenBlove/ICSharpCode.SharpZipLib.rar
Compress and PackageCode
/// <Summary>
/// Generate a compressed file
/// </Summary>
/// <Param name = "strzippath"> path of the generated ZIP file </param>
/// <Param name = "strziptopdirectorypath"> parent directory of the source file </param>
/// <Param name = "intziplevel"> T compression level </param>
/// <Param name = "strpassword"> decompress the password of the compressed package </param>
/// <Param name = "filesordirectoriespaths"> source file path </param>
/// <Returns> </returns>
Private bool zip (string strzippath, 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 <filesordirectoriespaths. 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 = 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 (0 );
}
Zipentry entry = new zipentry (strfilename );
Entry. datetime = datetime. now;
Zipoutputstream. putnextentry (entry );
Zipoutputstream. Write (buffer, 0, buffer. Length );
FS. Close ();
FS. Dispose ();
}
}
Catch
{
Continue;
}
}
Zipoutputstream. Finish ();
Zipoutputstream. Close ();
Return true;
}
Else
{
Return false;
}
}
Catch
{
Return false;
}
}
/// <Summary>
/// Gets the directory files.
/// </Summary>
/// <Param name = "strparentdirectorypath"> source file path </param>
/// <Param name = "allfilespath"> all file paths </param>
Private void getdirectoryfiles (string strparentdirectorypath, list <string> allfilespath)
{
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 );
}
}
Call
String strzippath = @ "D: \ consultsystem \ Mgr \ user.zip ";
String strziptopdirectorypath = @ "D: \ consultsystem \ Mgr \";
Int intziplevel = 6;
String strpassword = "";
String [] filesordirectoriespaths = new string [] {@ "D: \ consultsystem \ Mgr \ user.txt "};
Zip (strzippath, strziptopdirectorypath, intziplevel, strpassword, filesordirectoriespaths );