///
/// Call WinRar.exe compression and decompression.
/// summary>
Class WinRAR
{
String WinRAREXE;
String password;
/// /// Constructor. /// summary> /// WinRar.exe's directory (absolute path). Please copy WinRar.exe to a directory you have read and write, and then use this directory as a parameter. Otherwise it will not be executed. param> /// The password used to compress or decompression, if there is no password, use the other Password parameter constructor. Param> public Winrar (string directoryOfWinrarExe, string password) {directoryOfWinrarExe = CheckDirectoryName (directoryOfWinrarExe); this.winrarExe = directoryOfWinrarExe "Winrar.exe";
this.password = Checkpassword (Password);
/// /// Constructor /// summary> /// WinRar.exe's directory (absolute path). Please copy WinRar.exe to a directory you have read and write, and then use this directory as a parameter. Otherwise it will not be executed. param> public winrar (String Directoryofwinrarexe) {DirectoryOfwinrarexe = CheckDirectoryName (DirectoryOfwinRAREXE); this.winraRexe = DirectoryOfwinraRexe "WinRar.exe";
this.password = null;}
/// /// checks if the directory name is in conformity. /// summary> /// Directory name (absolute path) param> /// directory name (absolute path) returns> static string checkdirectoryName (String DirectoryName ) {IF (DirectoryName == null) {throw new exception ("Directory Name Can't be NULL.");
Regex regex = new regex (@ "^ [A-ZA-Z]: [^ / n] * // $"); if (! Regex.ismatch (directoryName)) {throw new exception (@ "directory name The absolute path must be ended with "/". Such as: C: / windows /. ");
Return DirectoryName;}
/// /// check if the password is in line with format. /// summary> /// Password param> /// password returns> static string checkpassword (string password) {if (password == null) { Throw new Exception ("Password is not null. If you don't pass the password, please use another PASSWORD parameter constructor.");} regex regex = new regex (@ "^ / w $"); if (! regex.ismatch (Password) {throw new exception ("Password Please use letters, numbers, and lower");}
Return Password;}
/// /// checks if the file name is expanded to .rar. /// summary> /// WinRAR file name (absolute path) param> /// WinRAR file name (absolute path) returns> static string checkrarfile String rarfile) {if (rarfile == null) {throw new exception ("file name is not null.");}
Regex regex = new regex (@ "^ [A-ZA-Z]: //. /.rar $"); if (! Regex.ismatch (rarfile)) {throw new exception (@ "file name Please use absolute The path must end with "extension .rar". Such as: c: /windows/aa.rar ");
Return rarfile;}
/// /// generates a temporary file (Backup.lst). Each line in this file includes a file name to be compressed. This file name is used to construct the WinRAR a backup.rar @ backup.rar. Please refer to WinRar.exe Help /// summary> /// to compress file name array. param> /// Temporary file name, used to construct the WinRAR a backup.rar @ backup.rar param> void createlstfile (String [] filestocompress, out string lstfile {string Path = WinRAREXE.SUBSTRING (0, WinraRexe.lastIndexof ("/") 1); string filename = datetime.now.tostring ("YYYYMMDDHHMMSS") ".lst"; lstfile = path filename;
StreamWriter SW = New StreamWriter (Path FileName, False); Foreach (String File in filestocompress) {sw.writeLine (file);}
SW.CLOSE ();
/// /// check is whether the file name is in line with format (absolute path). /// summary> /// file name (absolute path). param> static void checkfiles (string [] files) {regex regex = new regex (@ "[a-za-z]: //. /.. ");
Foreach (string file in files) {if (file == null) {throw new exception ("file name is not null.");}
If (! regex.ismatch (file)) {throw new exception, use an absolute path, and pay attention to the format. Such as: c: /windows/aa.txt ");}}
}
/// /// Compressed specified directory. /// summary> /// To compress the directory, including the subdirectory below. param> /// Compressed file name. param> /// Whether the source directory is deleted after compression. Param> public void Compress (string directoryToCompress, string rarFileToSave, bool deleteDirectoryToCompress) {directoryToCompress = CheckDirectoryName (directoryToCompress); rarFileToSave = CheckRarFile (rarFileToSave);
IF (file.exists (rarfiletosave)) {file.delete (RARFILETOSAVE);
Process p = new process (); // Requires the program name P.StartInfo.FileName = WinraRexe; // Parameter string arguments = @ "a -ep1 -r-- -inul -y"; if (Password! = Null ) {arguments = "-p" password;} arguments = "" rarFileToSave "" directoryToCompress; p.StartInfo.Arguments = arguments; p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; p.Start (); // Start while (! P.hasexited) {} p.WaitForexit (); if (deletedIRectoryTocompress) {Directory.delete (True);}} /// /// compresses the specified file list. /// summary> /// After the compressed file name (absolute path). param> /// Whether the source file list is deleted after compression. param> /// To compress the file list (absolute path). Param> public void Compress (string rarFileToSave, bool deleteSourceFiles, params string [] filesToCompress) {rarFileToSave = CheckRarFile (rarFileToSave); if (File.Exists (rarFileToSave)) {File.Delete (rarFileToSave);}
CheckFiles (filesToCompress); string lstFile; this.CreateLstFile (filesToCompress, out lstFile); Process p = new Process (); // need to start the program name p.StartInfo.FileName = winrarExe; // parameter string arguments = @ "a -EP1 -R -O "; if (Password! = null) {arguments =" -p " password;} arguments =" " RARFILETOSAVE " @ " LSTFILE; P.StartInfo.arguments = arguments; P .Startinfo.WindowStyle = processWindowStyle.hidden; p.Start (); // Start while (! P.hasexited) {} p.WaitForeXit (); if (deletesourcefiles) {Foreach (String file in filestocompress) file.delete (file) File.delete }
File.delete (Lstfile);
/// /// Decompress the specified RAR file. /// summary> /// RAR file (absolute path). parame> /// Unzip the saved directory. param> /// Delete the RAR file after decompression. Param> public void Decompress (string rarFileToDecompress, string directoryToSave, bool deleteRarFile) {directoryToSave = CheckDirectoryName (directoryToSave); rarFileToDecompress = CheckRarFile (rarFileToDecompress);
Process p = new process (); // Requires the program name p.startinfo.filename = WinraRexe; // Parameters string arguments = @ "x -inul -y-="; if (password! = Null) {Arguments = "-p" password;} arguments = " rarFiletodeCompress " DirectoryTOSave;
P.StartInfo.arguments = arguments; p.startinfo.windowstyle = processWindowStyle.hidden; p.Start (); // Start while (! p.hasexited) {} p.WaitForexit (); if (deleterarfile) {file.delete RARFILETODECOMPRESS;}}
}