C # extract RAR compressed files

Source: Internet
Author: User
Using system;
Using system. Collections. Generic;
Using system. text;
Using system. IO;
Using Microsoft. Win32;
Using system. diagnostics;

Namespace uni. unicustoms
{
Public class clswinrar
{
/// <Summary>
/// Whether WinRAR is installed
/// </Summary>
/// <Returns> </returns>
Static public bool exists ()
{
Registrykey the_reg = registry. localmachine. opensubkey (@ "SOFTWARE \ Microsoft \ Windows \ CurrentVersion \ app paths \ winrar.exe ");
Return! String. isnullorempty (the_reg.getvalue (""). tostring ());
}

/// <Summary>
/// Package it into RAR
/// </Summary>
/// <Param name = "patch"> </param>
/// <Param name = "rarpatch"> </param>
/// <Param name = "rarname"> </param>
Public void compressrar (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. localmachine. opensubkey (@ "SOFTWARE \ Microsoft \ Windows \ CurrentVersion \ app paths \ winrar.exe ");
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: test? 70821. 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)
{
Throw ex;
}
}
/// <Summary>
/// Extract
/// </Summary>
/// <Param name = "unrarpatch"> </param>
/// <Param name = "rarpatch"> </param>
/// <Param name = "rarname"> </param>
/// <Returns> </returns>
Public String uncompressrar (string unrarpatch, string rarpatch, string rarname)
{
String the_rar;
Registrykey the_reg;
Object the_obj;
String the_info;

Try
{
The_reg = registry. localmachine. opensubkey (@ "SOFTWARE \ Microsoft \ Windows \ CurrentVersion \ app paths \ winrar.exe ");
The_obj = the_reg.getvalue ("");
The_rar = the_obj.tostring ();
The_reg.close ();
// The_rar = the_rar.substring (1, the_rar.length-7 );

If (directory. exists (unrarpatch) = false)
{< br> directory. createdirectory (unrarpatch);
}< br> the_info = "X" + rarname + "" + unrarpatch + "-y";

processstartinfo the_startinfo = new processstartinfo ();
the_startinfo.filename = the_rar;
partitions = the_info;
the_startinfo.windowstyle = processwindowstyle. hidden;
the_startinfo.workingdirectory = rarpatch; // obtain the path of the compressed package.

process the_process = new process ();
the_process.startinfo = the_startinfo;
the_process.start ();
the_process.waitforexit ();
the_process.close ();
}< br> catch (exception ex)
{< br> throw ex;
}< br> return unrarpatch;
}< BR >}

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.