This article describes the method of C# to implement rar compression and decompression of files. Share it for your reference. The specific analysis is as follows:
This program uses the WinRAR program to compress files. For command line syntax, please refer to WinRAR Chinese Help.
/// Use WinRAR for compression/// </summary> /// <param name="path">The folder to be compressed (absolute path)</param>/// <param name="rarPath">Compressed .rar storage directory (absolute path)</param>/// <param name="rarName">The name of the compressed file (including the suffix)</param>/// <returns>true or false. Compression returns true if successful, otherwise, false. </returns>public bool RAR(string path, string rarPath, string rarName) { bool flag = false; string rarexe; // The complete path RegistryKey regkey; //Register key Object regvalue; //Key value string cmd; //WinRAR command parameters ProcessStartInfo startinfo; Process process; try { regkey = (@"Applications\\shell\open\command"); regvalue = (""); // The key value is "d:\Program Files\WinRAR\" "%1" rarexe = (); (); rarexe = (1, - 7); // d:\Program Files\WinRAR\ (path); //Compression command is equivalent to right-clicking on the folder to be compressed (path) ->WinRAR->Add to compressed file ->Enter compressed file name (rarName) cmd = ("a {0} {1} -r", rarName, path); startinfo = new ProcessStartInfo(); = rarexe; = cmd; //Set command parameters = ; //Hide WinRAR window = rarPath; process = new Process(); = startinfo; (); (); //Waiting for the process indefinitely if () { flag = true; } (); } catch (Exception e) { throw e; } return flag; } /// <summary> /// Decompress using WinRAR/// </summary> /// <param name="path">File decompression path (absolute)</param>/// <param name="rarPath">The directory (absolute path) of the .rar file to be decompressed</param>/// <param name="rarName">The .rar file name (including suffix) to be unzipped</param>/// <returns>true or false. Decompression successfully returns true, otherwise, false. </returns>public bool UnRAR(string path, string rarPath, string rarName) { bool flag = false; string rarexe; RegistryKey regkey; Object regvalue; string cmd; ProcessStartInfo startinfo; Process process; try { regkey = (@"Applications\\shell\open\command"); regvalue = (""); rarexe = (); (); rarexe = (1, - 7); (path); //The decompression command is equivalent to right-clicking on the file to be compressed (rarName) -> WinRAR->Decompress to the current folder cmd = ("x {0} {1} -y", rarName, path); startinfo = new ProcessStartInfo(); = rarexe; = cmd; = ; = rarPath; process = new Process(); = startinfo; (); (); if () { flag = true; } (); } catch (Exception e) { throw e; } return flag; } /// <summary> /// Use WinRAR for compression/// </summary> /// <param name="path">The folder to be compressed (absolute path)</param>/// <param name="rarPath">Compressed .rar storage directory (absolute path)</param>/// <param name="rarName">The name of the compressed file (including the suffix)</param>/// <returns>true or false. Compression returns true if successful, otherwise, false. </returns>public bool RAR(string path, string rarPath, string rarName) { bool flag = false; string rarexe; // The complete path RegistryKey regkey; //Register key Object regvalue; //Key value string cmd; //WinRAR command parameters ProcessStartInfo startinfo; Process process; try { regkey = (@"Applications\\shell\open\command"); regvalue = (""); // The key value is "d:\Program Files\WinRAR\" "%1" rarexe = (); (); rarexe = (1, - 7); // d:\Program Files\WinRAR\ (path); //Compression command is equivalent to right-clicking on the folder to be compressed (path) ->WinRAR->Add to compressed file ->Enter compressed file name (rarName) cmd = ("a {0} {1} -r", rarName, path); startinfo = new ProcessStartInfo(); = rarexe; = cmd; //Set command parameters = ; //Hide WinRAR window = rarPath; process = new Process(); = startinfo; (); (); //Waiting for the process indefinitely if () { flag = true; } (); } catch (Exception e) { throw e; } return flag; } /// <summary> /// Decompress using WinRAR/// </summary> /// <param name="path">File decompression path (absolute)</param>/// <param name="rarPath">The directory (absolute path) of the .rar file to be decompressed</param>/// <param name="rarName">The .rar file name (including suffix) to be unzipped</param>/// <returns>true or false. Decompression successfully returns true, otherwise, false. </returns>public bool UnRAR(string path, string rarPath, string rarName) { bool flag = false; string rarexe; RegistryKey regkey; Object regvalue; string cmd; ProcessStartInfo startinfo; Process process; try { regkey = (@"Applications\\shell\open\command"); regvalue = (""); rarexe = (); (); rarexe = (1, - 7); (path); //The decompression command is equivalent to right-clicking on the file to be compressed (rarName) -> WinRAR->Decompress to the current folder cmd = ("x {0} {1} -y", rarName, path); startinfo = new ProcessStartInfo(); = rarexe; = cmd; = ; = rarPath; process = new Process(); = startinfo; (); (); if () { flag = true; } (); } catch (Exception e) { throw e; } return flag; }
After using the cd command to enter the winrar installation directory under dos or cmd, enter unrar to get the following prompts:
Usage: unrar < Command> -<Switch 1> -<Switch N> <Compressed File> <File...>
<@ List file...> <Decompression path\>
<Command>
e
l[t,b] List compressed files [Technical Information, Concise]
p Print the file to the standard output device
Test compressed files
v[t,b] �
x
<Switch>
- Stop scanning
ac Clear archive properties after compression or decompression
ad
ap<format> Add path to compressed file
av- Disable user identity verification
c- Disable comment display
cfg- Disable read configuration
cl Convert the name to lowercase
cu �
dh �
ep Exclude paths from names
ep3 The extension path is the full path containing the drive letter
f
id[c,d,p,q] Disable message
ierr Send all messages to the standard error device
inul Disable all messages
ioff �
kb �
n<file> �
n@ �
n@<list> Include files in the specified file list
o+ �
o- �
oc �
or
ow �
p[Password] Set password
p- �
r
ri<P>[:<S>] Set priority (0-default, 1-min..15-maximum) and sleep time in milliseconds
sl<size> Processing files smaller than the specified size
sm<size> Processing files exceeding the specified size
ta<date> File modified after adding date <date>, date format YYYYMMDDHHMMSS
tb<date> File modified before adding date <date>, date format YYYYMMDDHHMMSS
tn<time> Add <time> to future files
to<Time> Add <Time> Previous File
ts<m,c,a>[N] Time to save or restore file (modify, create, access)
Update the file
v
ver[n] File version control
vp �
x<file> �
x@ �
x@<list> Exclude files from the specified list file
y �
Specific examples of using WinRAR to deconstruct files under DOS (cmd):
Assume that there is a , in the disk d , to decompress it into the hello folder in the disk F , then enter the following command under dos:
unrar x d:\ F:\hello\
I hope this article will be helpful to everyone's C# programming.