Call WinRar.exe compression and decompression

xiaoxiao2021-03-06  53

///

/// Call WinRar.exe compression and decompression.

///

Class WinRAR

{

String WinRAREXE;

String password;

///

/// Constructor. /// /// 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. /// The password used to compress or decompression, if there is no password, use the other Password parameter constructor. public Winrar (string directoryOfWinrarExe, string password) {directoryOfWinrarExe = CheckDirectoryName (directoryOfWinrarExe); this.winrarExe = directoryOfWinrarExe "Winrar.exe";

this.password = Checkpassword (Password);

///

/// Constructor /// /// 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. public winrar (String Directoryofwinrarexe) {DirectoryOfwinrarexe = CheckDirectoryName (DirectoryOfwinRAREXE); this.winraRexe = DirectoryOfwinraRexe "WinRar.exe";

this.password = null;}

///

/// checks if the directory name is in conformity. /// /// Directory name (absolute path) /// directory name (absolute path) 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. /// /// Password /// password 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. /// /// WinRAR file name (absolute path) /// WinRAR file name (absolute path) 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 /// /// to compress file name array. /// Temporary file name, used to construct the WinRAR a backup.rar @ backup.rar 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). /// /// file name (absolute path). 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. /// /// To compress the directory, including the subdirectory below. /// Compressed file name. /// Whether the source directory is deleted after compression. 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. /// /// After the compressed file name (absolute path). /// Whether the source file list is deleted after compression. /// To compress the file list (absolute path). 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. /// /// RAR file (absolute path). /// Unzip the saved directory. /// Delete the RAR file after decompression. 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;}}

}

转载请注明原文地址:https://www.9cbs.com/read-82677.html

New Post(0)