Use C # To Call WinRAR for compression and decompression

Source: Internet
Author: User
Public   Void Rarsave ( String Patch, String Rarpatch, String Rarname)
... {
String the_rar;
Registrykey the_reg;
Object the_obj;
String the_info;
Processstartinfo the_startinfo;
Process the_process;
Try
... {
The_reg = Registry. classesroot. opensubkey ( @" Applicationswinrar.exe shellopencommand " );
The_obj = The_reg.getvalue ( "" );
The_rar = The_obj.tostring ();
The_reg.close ();
The_rar = The_rar.substring ( 1 , The_rar.length -   7 );
Directory. createdirectory (patch );
// Command Parameters
// The_info = "A" + rarname + "" + @ "C: test70821.txt "; // File compression
The_info =   " A "   + Rarname +   " "   + Patch +   " -R " ;;
The_startinfo =   New Processstartinfo ();
The_startinfo.filename = The_rar;
The_startinfo.arguments = The_info;
The_startinfo.windowstyle = Processwindowstyle. hidden;
// Directory for storing packaged files
The_startinfo.workingdirectory = Rarpatch;
The_process =   New Process ();
The_process.startinfo = The_startinfo;
The_process.start ();
The_process.waitforexit ();
The_process.close ();
}
Catch (Exception ex)
... {
ThrowEx;
}
}

Public   String Unrar ( String Unrarpatch, String Rarpatch, String Rarname)
... {
String the_rar;
Registrykey the_reg;
Object the_obj;
String the_info;
Processstartinfo the_startinfo;
Process the_process;
Try
... {
The_reg = Registry. classesroot. opensubkey ( @" Applicationswinrar.exe shellopencommand " );
The_obj = The_reg.getvalue ( "" );
The_rar = The_obj.tostring ();
The_reg.close ();
The_rar = The_rar.substring ( 1 , The_rar.length -   7 );
Directory. createdirectory (server. mappath (unrarpatch ));
The_info =   " E "   + Rarname +   " "   + Server. mappath (unrarpatch) +   " -Y " ;
The_startinfo =   New Processstartinfo ();
The_startinfo.filename = The_rar;
The_startinfo.arguments = The_info;
The_startinfo.windowstyle = Processwindowstyle. hidden;
The_startinfo.workingdirectory = Server. mappath (rarpatch ); // Obtain the compressed package path
The_process =   New Process ();
The_process.startinfo = The_startinfo;
The_process.start ();
The_process.waitforexit ();
The_process.close ();
}
Catch (Exception ex)
... {
ThrowEx;
}
Return Server. mappath (unrarpatch );
}

RAR parameters:

1. Compression commands
1. Compress temp.txt to temp.rar rar a temp.rar temp.txt
2. compress all files in the current directory to temp.rar rar a temp.rar *.*
3rd, compress all the files in the current directory and all their sub-directories to temp.rar A temp.rar *. *-R
4. compress all files in the current directory and all their sub-directories to temp.rar, and add the password 123rar A temp.rar *. *-r-p123

Ii. decompress the command
1. Extract temp.rar to the C: \ Temp Directory RAR e temp.rar c: \ temprar E *. rar c: \ Temp (batch operation supported)
2. Extract temp.rar to the C: \ tempdirectory, and extract the directory structure and directory structure 1 in temp.rar.

Compressing the file content of the test directory and Its subdirectories
Wzzip test.zip test-r-p
WinRAR A test.rar test-R

Delete the *. txt file in the compressed package
Wzzip test.zip *. txt-d
WinRAR D test.rar *. txt

Refresh the files in the compressed package, that is, add files that already exist in the compressed package but are updated
Wzzip test.zip test-F
WinRAR F test.rar Test

Update the files in the compressed package, that is, add the files that already exist in the compressed package but are updated and the new files
Wzzip test.zip test-u
WinRAR U test.rar Test

Move the file to the compressed package, that is, add the file to the compressed package and then delete the compressed file.
Wzzip test.zip-r-p-m
WinRAR M test.rar test-R

Add all *. EXE files to the compressed file, but exclude A or B.
File with the start name
Wzzip test *. exe-XF *. *-xB *.*
WinRAR a test *. exe-XF *. *-xB *.*

Compress with a password
Wzzip test.zip Test
-S123. Note that the password is case sensitive. Open the compressed file with a password in the graphic interface and you will see the + mark (Fig. 1 ).
WinRAR A test.rar test-p123
-R. Note that the password is case sensitive. Open the compressed file with a password in the graphic interface and you will see the * mark (figure 2 ).

Sort by name and display compressed package files in a brief list
Wzzip test.zip-VBN
RAR l test.rar

Lock the compressed package to prevent any modifications to the compressed package in the future.
No corresponding command
WinRAR K test.rar

Create a kb compressed package
No corresponding command
WinRAR A-v360 Test

Decompress the directory information
Wzunzip test-d
WinRAR X test-R

Extract files without directory information
Wzunzip Test
WinRAR E test

Decompress the file to the specified directory. If the directory does not exist, it is automatically created.
Wzunzip test newfolder
WinRAR X test newfolder

Decompress the file and confirm to overwrite the file
Wzunzip test-y
WinRAR X test-y

Decompress a specific file
Wzunzip test *. txt
WinRAR X test *. txt

Decompress the Update file of an existing file
Wzunzip test-F
WinRAR X test-F

Decompress the update files and new files of existing files
Wzunzip test-n
WinRAR X test-u

Decompress files in batches
Wzunzip *. Zip
WinRAR E *. rar

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.