Dynamic compression and decompression of cve-rar

Source: Internet
Author: User
Tags cve

As shown in the following figure, the compression and decompression of .rar are implemented. The Code is as follows:

 

 

Using Microsoft. win32; <br/> using system. diagnostics; </P> <p> // <summary> <br/> // use WinRAR for compression <br/> /// </Summary> <br/>/ // <Param name = "path"> folder to be compressed (absolute path) </param> <br/> // <Param name = "rarpath"> compressed. RAR storage directory (absolute path) </param> <br/> // <Param name = "rarname"> name of the compressed file (including the suffix) </param> <br/> /// <returns> true or false. True is returned if compression is successful. Otherwise, false is returned. </Returns> <br/> Public bool RAR (string path, string rarpath, string rarname) <br/>{< br/> bool flag = false; <br/> string rarexe; // full path of winrar.exe <br/> registrykey regkey; // registry key <br/> Object regvalue; // key value <br/> string cmd; // WinRAR command parameter <br/> processstartinfo startinfo; <br/> process; <br/> try <br/> {<br/> regkey = registry. classesroot. opensubkey (@ "applications/winrar.exe/Shell/ope N/command "); <br/> regvalue = regkey. getvalue (""); // the key value is "D:/program files/WinRAR/winrar.exe" "% 1" <br/> rarexe = regvalue. tostring (); <br/> regkey. close (); <br/> rarexe = rarexe. substring (1, rarexe. length-7); // D:/program files/WinRAR/winrar.exe </P> <p> directory. createdirectory (PATH); <br/> // The compression command, which is equivalent) right-click on the page and choose WinRAR> Add to compressed file> enter the compressed file name (rarname) <br/> cmd = string. format ("A {0} {1}-R ", <Br/> rarname, <br/> path); <br/> startinfo = new processstartinfo (); <br/> startinfo. filename = rarexe; <br/> startinfo. arguments = cmd; // set command parameters <br/> startinfo. windowstyle = processwindowstyle. hidden; // hide the WinRAR window </P> <p> startinfo. workingdirectory = rarpath; <br/> process = new process (); <br/> process. startinfo = startinfo; <br/> process. start (); <br/> process. waitforexit (); // wait for the process win indefinitely Rar.exe exits <br/> If (process. hasexited) <br/>{< br/> flag = true; <br/>}< br/> process. close (); <br/>}< br/> catch (exception e) <br/>{< br/> throw E; <br/>}< br/> return flag; <br/>}< br/> /// <summary> <br/> // decompress with WinRAR <br/> /// </Summary> <br/> /// <Param name = "path"> file decompression path (absolute) </param> <br/> // <Param name = "rarpath"> the file to be extracted. RAR file storage directory (absolute path) </param> <br/> // <Param name = "rarnam E "> Name Of The. rar file to be extracted (including the suffix) </param> <br/> // <returns> true or false. True is returned for successful decompression. Otherwise, false is returned. </Returns> <br/> Public bool unrar (string path, string rarpath, string rarname) <br/>{< br/> bool flag = false; <br/> string rarexe; <br/> registrykey regkey; <br/> Object regvalue; <br/> string cmd; <br/> processstartinfo startinfo; <br/> process; <br/> try <br/> {<br/> regkey = registry. classesroot. opensubkey (@ "applications/winrar.exe/Shell/Open/command"); <br/> regvalue = regkey. getvalue (""); <br/> rarexe = regvalue. tostring (); <br/> regkey. close (); <br/> rarexe = rarexe. substring (1, rarexe. length-7); </P> <p> directory. createdirectory (PATH); <br/> // decompress the command, which is equivalent) right-click and choose WinRAR> extract to the current folder <br/> cmd = string. format ("X {0} {1}-y", <br/> rarname, <br/> path); <br/> startinfo = new processstartinfo (); <br/> startinfo. filename = rarexe; <br/> startinfo. arguments = cmd; <br/> startinfo. windowstyle = processwindowstyle. hidden; </P> <p> startinfo. workingdirectory = rarpath; <br/> process = new process (); <br/> process. startinfo = startinfo; <br/> process. start (); <br/> process. waitforexit (); <br/> If (process. hasexited) <br/>{< br/> flag = true; <br/>}< br/> process. close (); <br/>}< br/> catch (exception e) <br/>{< br/> throw E; <br/>}< br/> return flag; <br/>}< br/>

 

I read it again yesterday and found that if there are spaces in the path (such as: D:/program files/), the compression and decompression will be problematic, and it took me a long time, finally, I had to interview in cmd for half a day and found that double quotation marks can be added to the path with spaces. In the code, directory. createdirectory (PATH); then, Judge path and rarname. If there is space, add Path = "/" "+ path + "/"";

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 RAR e temp.rar C:/temprar E *. rar C:/TEMP directory (batch operations are 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

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.