ASP. NET to generate a compressed file (RAR package)

Source: Internet
Author: User

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 );

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.