C # perform operations on compressed files (RAR, zip, Gzip, winform/Asp.net)

Source: Internet
Author: User

Starting from. NET 2.0, Microsoft has provided a namespace of system. Io. compression, which contains gzipstream and deflatestream. Both of them have the compression and decompression functions. However, they do not support this function.ZipAndRARAnd other common compressed files. BecauseRARIt is copyrighted and does not support it. HoweverZipIt is open-source, and it is a little selfish if it is not supported (it is estimated that Microsoft is doing it for itself)CabFormat)

For the RAR format, we can directly use the rar.exe/unrar.exe file of winrar (this file is a console program in the installation directory of WinRAR). In this case, it can only be used for winform orElevation of PrivilegeCan be used in Asp.net;

For the ZIP format, we recommend that you use the native C # class library. This method can be applied to Asp.net (with directory write permission). I recommend you use the free open-source sharpziplib.

To decompress and compress rar.exe, the following command line format table is provided for ease of understanding:

RAR command line usage:

RAR <command>-<switch 1>-<switch n> <compressed file> <file...> <@ list file...> <decompress path \>

RARCommand list:

 

RARSwitch list:

RAR example:

1. rar a file. ext
If file.rardoes not exist, the file.rarfile will be created. If file. Ext already exists in the file.rar compressed package, the file. Ext In the compressed package will be updated.

2. rar a file D: \ *. ext

Add all ext files (excluding self-folders) in drive D to the compressed package

3. rar x fonts *. TTF
Extract the *. TTF font file from the compressed file to the current folder.

4. rar x fonts *. TTF newfonts \

Extract the *. TTF font file from the compressed file to the newfonts directory.

5. rar e-p password test.rar

Decompress the password test.rar file to the current folder.

Csung uses rar.exe to decompress an rarfile to the temporary directory of the system:
// Obtain the temporary system directory string systempdir = path. gettemppath (); // the path of the file to be decompressed. Set string rarfilepath = @ "D: \ test.rar"; // determine the directory to decompress, is the string unrardestpath = path in the directory with the same name as the original compressed file in the temporary system folder. combine (systempdir, path. getfilenamewithoutextension (rarfilepath); // combine the complete shell Format String shellarguments = string. format ("X-O + \" {0} \ "\" {1} \ "", rarfilepath, unrardestpath ); // use process to call using (process unrar = new process () {unrar. startinfo. filename = "rar.exe"; unrar. startinfo. arguments = shellarguments; // hide the RAR window unrar. startinfo. windowstyle = processwindowstyle. hidden; unrar. start (); // wait until unrar is decompressed. waitforexit (); unrar. close ();} // calculate the number of extracted directories and files. directoryinfo di = new directoryinfo (unrardestpath); MessageBox. show (string. format ("decompressed, decompressed: {0} directories, {1} Files", Di. getdirectories (). length, Di. getfiles (). length ));

Sample Code download

Original article: http://www.zu14.cn/2008/12/31/net_package/

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.