C # method to implement RAR compression and decompression files

Source: Internet
Author: User
Tags bool rar time in milliseconds tostring

This paper illustrates the method of C # to implement RAR compression and decompression files. Share to everyone for your reference. The specific analysis is as follows:

This program uses the WinRAR program to compress files, and command line syntax can refer to WinRAR Chinese help.

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26-27--28 29---30 31--32 33 34 35 36 37 38-39 40 41 42 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 5, 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 11 9 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148-149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179-18 0 181 182 183 184 185 186 187 Use WinRAR to compress///</summary>///<param name= "path" > Folders to be Compressed (absolute path) </param>///<param name= "Ra Rpath "> Compressed. RAR storage Directory (absolute path) </param>///<param name=" Rarname "> Compressed file name (including suffix) </param>///< Returns>true or False. Compression successfully returns TRUE, otherwise, false. </returns> public bool RAR (string path, String Rarpath, String rarname) {BOOL flag = false; string Rarexe;//winrar The full path of the. exe RegistryKey regkey; Registry key Object Regvalue; Key value string cmd; WinRAR command Parameters ProcessStartInfo startinfo; Process process; try {regkey = Registry.ClassesRoot.OpenSubKey (@ "Applicationswinrar.exeshellopencommand"); regvalue = RegKey. GetValue (""); The key value is "D:program FilesWinRARWinRAR.exe" "%1" Rarexe = Regvalue. ToString (); RegKey. Close (); Rarexe = Rarexe. Substring (1, Rarexe. LENGTH-7); D:program FilesWinRARWinRAR.exe directory.createdirectory (path); The compression command, which is equivalent to right-clicking on the folder (path) to be compressed->winrar-> add to the compressed file-> input Compressed file name (rarname) cmd = string. Format ("a {0} {1}-R", Rarname, Path); Startinfo = new ProcessStartInfo (); StartInfo. FileName = Rarexe; StartInfo. Arguments = cmd; Sets the command parameter StartInfo. WindowStyle = Processwindowstyle.hidden; Hides the WinRAR window startinfo. WorkingDirectory = Rarpath; Process = new process (); Process. StartInfo = StartInfo; Process. Start (); Process. WaitForExit (); Wait indefinitely for the process to exit the IF (Winrar.exe). hasexited) {flag = true;} process. Close (); catch (Exception e) {throw e;} return flag; ///<summary>///using WinRAR to extract///</summary>///<param name= "path" > File decompression Path (absolute) </param>///& Lt;param name= "Rarpath" > the directory of the. rar file to be uncompressed (absolute path) </param>///<param name= "Rarname" > the. rar that will be decompressed File name (including suffix) </param>///<returns>true or False. The 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 = Registry.ClassesRoot.OpenSubKey (@) ApplicatIonswinrar.exeshellopencommand "); Regvalue = RegKey. GetValue (""); Rarexe = Regvalue. ToString (); RegKey. Close (); Rarexe = Rarexe. Substring (1, Rarexe. LENGTH-7); Directory.CreateDirectory (path); Extract the command, which is equivalent to compressing the file (rarname) on the dot right button->winrar-> extract 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; ///<summary>///uses WinRAR for compression///</summary>///<param name= "path" > Folders to be Compressed (absolute path) </param>/ <param name= "Rarpath" > Compressed. RAR storage Directory (absolute path) </param>///<param name= "Rarname" > Compressed file name (including suffix) </ Param>///<returns>true or False. Compression successfully returns TRUE, otherwise, false. </returns&gT public bool RAR (string path, String Rarpath, String rarname) {BOOL flag = false; string Rarexe;//winrar.exe full path regist Rykey RegKey; Registry key Object Regvalue; Key value string cmd; WinRAR command Parameters ProcessStartInfo startinfo; Process process; try {regkey = Registry.ClassesRoot.OpenSubKey (@ "Applicationswinrar.exeshellopencommand"); regvalue = RegKey. GetValue (""); The key value is "D:program FilesWinRARWinRAR.exe" "%1" Rarexe = Regvalue. ToString (); RegKey. Close (); Rarexe = Rarexe. Substring (1, Rarexe. LENGTH-7); D:program FilesWinRARWinRAR.exe directory.createdirectory (path); The compression command, which is equivalent to right-clicking on the folder (path) to be compressed->winrar-> add to the compressed file-> input Compressed file name (rarname) cmd = string. Format ("a {0} {1}-R", Rarname, Path); StartInfo = new ProcessStartInfo (); StartInfo. FileName = Rarexe; StartInfo. Arguments = cmd; Sets the command parameter StartInfo. WindowStyle = Processwindowstyle.hidden; Hides the WinRAR window startinfo. WorkingDirectory = Rarpath; Process = new process (); Process. StartInfo = StartInfo; Process. Start (); Process. WaitForExit (); //Wait indefinitely for the process to exit the IF (Winrar.exe). hasexited) {flag = true;} process. Close (); catch (Exception e) {throw e;} return flag; ///<summary>///using WinRAR to extract///</summary>///<param name= "path" > File decompression Path (absolute) </param>///& Lt;param name= "Rarpath" > the directory of the. rar file to be uncompressed (absolute path) </param>///<param name= "Rarname" > the. rar that will be decompressed File name (including suffix) </param>///<returns>true or False. The 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 = Registry.ClassesRoot.OpenSubKey (@ "Applicationswinrar.exeshellopencommand"); regvalue = RegKey. GetValue (""); Rarexe = Regvalue. ToString (); RegKey. Close (); Rarexe = Rarexe. Substring (1, Rarexe. LENGTH-7); Directory.CreateDirectory (path); Extract the command, which is equivalent to compressing the file (rarname) on the dot right button->winrar-> extract 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; }

In DOS or cmd under the CD command to enter the WinRAR installation directory, input Unrar can come out the following prompts:

Usage: Unrar < command >-< switch 1>-< switch n> < compressed file > < file ...>

<@ List File ...> < decompression path >

< command >

E Extract compressed files to current directory

L[T,B] List compressed files [technical information, concise]

P print files to standard output devices

T Test compressed file

V[T,B] Detailed list of compressed files [technical information, concise]

x extract files with absolute path

< switch >

-Stop Scanning

Clear Archive Properties After AC compression or decompression

Ad Add compressed file name to target path

ap< format > Add path to Compressed file

av-Disable user authentication

C-Disable Comment display

cfg-Disable Read configuration

CL name conversion to lowercase

CU name converted to uppercase

DH Open Shared File

EP excludes path from name

The EP3 extension path is the full path containing the letter

F Refresh File

ID[C,D,P,Q] Disable message

Ierr send all messages to standard error devices

Inul Disable all messages

Ioff turn off PC Power after completing one operation

KB Keep corrupted uncompressed files

n< files > contains only the specified files

n@ reads the file name from the standard input device to include

n@< list > Include files in the specified file list

o+ Overwrite existing file

o does not overwrite existing files

OC Set NTFS Compression properties

or automatically renames a file

ow save or restore file owners and groups

p[Password] Set password

P-Don't ask for password

R Recursive subdirectory

Ri

[:] Set Priority (0-default, 1-Min.). 15-maximum) and sleep time in milliseconds

sl< size > Handling files smaller than the specified size

sm< size > Handling files larger than the specified size

ta< Date > Add date < date > modified file, date format YYYYMMDDHHMMSS

tb< Date > Add Date < date > Previous modified file, date format YYYYMMDDHHMMSS

tn< time > Add < time > later files

to< time > Add < time > previous files

Ts[n] Save or restore file time (modify, create, access)

U Update files

V List all volumes

Ver[n] File version control

VP Pause before each volume

x< file > exclude specified files

x@ read the file name to exclude from the standard input device

x@< list > Exclude files in a specified list file

Y Suppose the answer to all inquiries is

In DOS (cmd) use WinRAR to extract the specific examples of the file:

Suppose that there is a winrar.rar in D disk, to extract to the Hello folder in the F disk, then enter the following command in DOS:

Unrar x D:winrar.rar F:hello

I hope this article will help you with the C # program.

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.