C # RAR compressed and decompressed files

Source: Internet
Author: User
Tags time in milliseconds

This program uses the WinRAR program to compress the file. For command line syntax, see WinRAR help.

/// <Summary>
  1. /// Use WinRAR for compression
  2. /// </Summary>
  3. /// <Param name = "path"> folder to be compressed (absolute path) </param>
  4. /// <Param name = "rarpath"> compressed. rar directory (absolute path) </param>
  5. /// <Param name = "rarname"> name (including suffix) of the compressed file </param>
  6. /// <Returns> true or false. True is returned if compression is successful. Otherwise, false is returned. </Returns>
  7. PublicBool RAR (string path, string rarpath, string rarname)
  8. {
  9. Bool flag =False;
  10. String rarexe; // The complete path of winrar.exe
  11. Registrykey regkey; // registry key
  12. Object regvalue; // key value
  13. String cmd; // WinRAR Command Parameters
  14. Processstartinfo startinfo;
  15. Process process;
  16. Try
  17. {
  18. Regkey = registry. classesroot. opensubkey (@ "applications \ winrar.exe \ shell \ open \ command ");
  19. Regvalue = regkey. getvalue (""); // the key value is "D: \ Program Files \ WinRAR \ winrar.exe" "% 1"
  20. Rarexe = regvalue. tostring ();
  21. Regkey. Close ();
  22. Rarexe = rarexe. substring (1, rarexe. Length-7); // D: \ Program Files \ WinRAR \ winrar.exe
  23. Directory. createdirectory (PATH );
  24. // The compression command, which is equivalent to right-clicking on the folder (PATH) to be compressed-> WinRAR-> adding to the compressed file-> entering the compressed file name (rarname)
  25. Cmd = string. Format ("A {0} {1}-R", rarname, PATH );
  26. Startinfo =NewProcessstartinfo ();
  27. Startinfo. filename = rarexe;
  28. Startinfo. Arguments = cmd; // set Command Parameters
  29. Startinfo. windowstyle = processwindowstyle. Hidden; // hide the WinRAR window
  30. Startinfo. workingdirectory = rarpath;
  31. Process =NewProcess ();
  32. Process. startinfo = startinfo;
  33. Process. Start ();
  34. Process. waitforexit (); // wait for the winrar.exe process to exit indefinitely
  35. If(Process. hasexited)
  36. {
  37. Flag =True;
  38. }
  39. Process. Close ();
  40. }
  41. Catch(Exception E)
  42. {
  43. ThrowE;
  44. }
  45. ReturnFlag;
  46. }
  47. /// <Summary>
  48. /// Decompress the package using WinRAR
  49. /// </Summary>
  50. /// <Param name = "path"> extract the file path (absolute) </param>
  51. /// <Param name = "rarpath"> storage directory of the. rar file to be decompressed (absolute path) </param>
  52. /// <Param name = "rarname"> Name Of The. rar file to be decompressed (including the suffix) </param>
  53. /// <Returns> true or false. True is returned for successful decompression. Otherwise, false is returned. </Returns>
  54. PublicBool unrar (string path, string rarpath, string rarname)
  55. {
  56. Bool flag =False;
  57. String rarexe;
  58. Registrykey regkey;
  59. Object regvalue;
  60. String cmd;
  61. Processstartinfo startinfo;
  62. Process process;
  63. Try
  64. {
  65. Regkey = registry. classesroot. opensubkey (@ "applications \ winrar.exe \ shell \ open \ command ");
  66. Regvalue = regkey. getvalue ("");
  67. Rarexe = regvalue. tostring ();
  68. Regkey. Close ();
  69. Rarexe = rarexe. substring (1, rarexe. Length-7 );
  70. Directory. createdirectory (PATH );
  71. // Decompress the command, which is equivalent to right-clicking on the file to be compressed (rarname)-> WinRAR-> decompressing to the current folder
  72. Cmd = string. Format ("X {0} {1}-y", rarname, PATH );
  73. Startinfo =NewProcessstartinfo ();
  74. Startinfo. filename = rarexe;
  75. Startinfo. Arguments = cmd;
  76. Startinfo. windowstyle = processwindowstyle. hidden;
  77. Startinfo. workingdirectory = rarpath;
  78. Process =NewProcess ();
  79. Process. startinfo = startinfo;
  80. Process. Start ();
  81. Process. waitforexit ();
  82. If(Process. hasexited)
  83. {
  84. Flag =True;
  85. }
  86. Process. Close ();
  87. }
  88. Catch(Exception E)
  89. {
  90. ThrowE;
  91. }
  92. ReturnFlag;
  93. }
/// <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"> name of the compressed file (including the suffix) </param> /// <returns> true or false. True is returned if compression is successful. Otherwise, false is returned. </Returns> Public bool RAR (string path, string rarpath, string rarname) {bool flag = false; string rarexe; // The complete path of winrar.exe is registrykey regkey; // registry key object regvalue; // key value string cmd; // The WinRAR command parameter processstartinfo startinfo; process; try {regkey = registry. classesroot. opensubkey (@ "applications \ winrar.exe \ shell \ open \ command"); regvalue = regkey. getvalue (""); // the key value is "D: \ Program Files \ WinRAR \ Winrar.exe "" % 1 "rarexe = regvalue. tostring (); regkey. close (); rarexe = rarexe. substring (1, rarexe. length-7); // D: \ Program Files \ WinRAR \ winrar.exe directory. createdirectory (PATH); // The compression command, which is equivalent to right-clicking on the folder (PATH) to be compressed-> WinRAR-> adding to the compressed file-> entering the compressed file name (rarname) cmd = string. format ("A {0} {1}-R", rarname, PATH); startinfo = new processstartinfo (); startinfo. filename = rarexe; startinfo. arguments = cmd; // set Command Parameters Startinfo. windowstyle = processwindowstyle. hidden; // hide the startinfo WinRAR window. workingdirectory = rarpath; process = new process (); process. startinfo = startinfo; process. start (); process. waitforexit (); // wait for the process winrar.exe to exit if (process. hasexited) {flag = true;} process. close () ;}catch (exception e) {Throw e ;}return flag ;} /// <summary> /// decompress the file using WinRAR /// </Summary> /// <Param name = "path"> Extract path (absolute) </param> // <Param name = "rarpath"> to decompress. RAR file storage directory (absolute path) </param> // <Param name = "rarname"> to be decompressed. RAR file name (including suffix) </param> // <returns> true or false. True is returned for successful decompression. Otherwise, false is returned. </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 = registry. classesroot. opensubkey (@ "applications \ winrar.exe \ shell \ open \ command"); regvalue = regkey. getvalue (""); rarexe = regvalue. tostring (); regkey. close (); rarexe = rarexe. substring (1, rarexe. length-7); directory. createdirectory (PATH); // decompress the command, which is equivalent to right-click the file to be compressed (rarname)-> WinRAR-> decompress it to the current folder cmd = string. format ("X {0} {1}-y", rarname, PATH); startinfo = new processstartinfo (); startinfo. filename = rarexe; startinfo. arguments = cmd; startinfo. windowstyle = processwindowstyle. hidden; startinfo. workingdirectory = rarpath; process = new process (); process. startinfo = startinfo; process. start (); process. waitforexit (); If (process. hasexited) {flag = true;} process. close () ;}catch (exception e) {Throw e ;}return flag ;}

Run the CD command in DOS or cmd to enter the WinRAR installation directory. Enter unrar and the following prompt is displayed:

Usage: unrar <command>-<switch 1>-<switch n> <compressed file> <file...>
<@ List file...> <decompress path \>

<Command>
E. decompress the compressed file to the current directory.
L [T, B] list compressed files [technical information, concise]
P print the file to the standard output device
T test compressed files
V [T, B] detailed list of compressed files [technical information, concise]
X decompress the file using the absolute path

<Switch>
-Stop Scanning
After the AC is compressed or unzipped, the archive attributes are cleared.
AD: Add compressed file names to the target path
AP <format> Add path to compressed file
AV-Disable User identity verification
C-Disable annotation display
CFG-Disable read Configuration
CL name to lowercase
Convert Cu name to uppercase
DH open shared files
EP exclusion path from name
The EP3 extension path is the full path containing the drive letter.
F. Refresh the file
Id [c, d, p, q] disable messages
Ierr sends all messages to standard error Devices
Inul disables all messages
After ioff completes an operation, turn off the PC power supply.
KB retains damaged decompressed files
N <File> only contains the specified file
N @ read the file name from the standard input device to include
N @ <list> include files in the specified file list
O + overwrite existing files
O-Do Not Overwrite existing files
Set NTFS compression attribute for OC
Or automatically rename the file
Ow save or restore the file owner and group
P [Password] Set Password
P-No Password inquiry
R recursive subdirectory
RI <p> [: <S>] sets the priority (0-default, 1-minimum... 15-maximum) and sleep time in milliseconds.
SL <size> process files smaller than the specified size
SM <size> processes files larger than the specified size
File Modified after ta <date> Add date <date>, in the format of yyyymmddhhmmss
File Modified before TB <date> Add date <date>, in the format of yyyymmddhhmmss
Files after tn <time> Add <time>
To <time> add files earlier than <time>
TS <m, C, A> [N] file retention or recovery time (modify, create, access)
U Update file
V. list all volumes
Ver [N] file version control
VP is paused before each volume
X <File> exclude specified files
X @ read the file name to be excluded from the standard input device
X @ <list> exclude objects in a specified list file
Y. If yes

Example of how to use WinRAR to decompress a file in DOS (CMD:
Suppose there is a winrar.rar in drive D. to decompress it to the hello folder in drive F, enter the following command under DOS:
Unrar x D: \ winrar.rar F: \ Hello \

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.