The principle of this article is to use the Windows platform-installed WinRAR (WinZip) to implement calls to C # programs (Note: WinRAR compression decompression WinZip is also applicable).
Let's take a look at the support invocation command of WinRAR (WINZIP) itself:
Compression command: a {0} {1}-r "{0}: File name after compression | {1}: File physical path to be compressed "
Ex: "A Your sister. rar f:\\mm-r" (meaning to compress the folder of MM under F disk to "your sister. rar" file)
Extract command: x {0} {1}-y "{0}: File name to Unzip | {1}: Physical path of the file to be extracted "
Ex: "X-sister. rar f:\\-y" (The physical path of the file to be compressed: "F:\\-yao sister. rar")
Parameter description
Parameters |
Meaning |
A |
Adding files to a compressed package |
X |
Extracting compression from a compressed package with a full path |
WINZIP (WinRAR) calls the generic class
Using system;using system.collections.generic;using system.text;//--------------usingusing system.diagnostics; Using microsoft.win32;using system.io;///<summary>///name:stone///datetime:2011/12/31 16:39:26/// Description:winrar Compression//</summary>public class winrarcsharp{//WinRAR Install Registry KEY Private Const string Winrar_key = @ "Winrar.zip\shell\open\command"; <summary>////use WinRAR to compress///</summary>/<param name= "path" > the folder to be compressed (absolute path) </para m>//<param name= "Rarpath" > Compressed. RAR storage Directory (absolute path) </param>//<param name= "Rarname" > The name of the compressed file (including suffix) </param>///<returns>true or FALSE. Compression successfully returns true, and vice versa, false. </returns> public bool RAR (string path, String Rarpath, String rarname) {BOOL flag = false; String Rarexe; The full path of WinRAR.exe RegistryKey regkey; Registry key Object Regvalue; Key value string cmd; WinRAR Command parameter ProcessStartInfo startinfo; Process process; try {regkey = Registry.ClassesRoot.OpenSubKey (Winrar_key); 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, equivalent to the right-click on the folder to be Compressed (path),->winrar-> add to the compressed file, enter the compressed filename (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 (); Waits indefinitely for the process to Winrar.exe exit if (process. hasexited) {flag = true; } process. Close (); } catch (Exception e) {throw e; } return flag; }///<summary>///use WinRAR to decompress///</summary>/<param name= "path" > File decompression Path (absolute) </par am>//<param name= "Rarpath" > The storage directory (absolute path) of the. rar file that will be decompressed </param>//<param name= "Rarname" > to be uncompressed The. rar file name (including suffix) </param>///<returns>true or FALSE. The decompression successfully returns true, and vice versa, 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 (Winrar_key); Regvalue = RegKey. GeTValue (""); Rarexe = Regvalue. ToString (); RegKey. Close (); Rarexe = Rarexe. Substring (1, Rarexe. LENGTH-7); Directory.CreateDirectory (path); Unzip the command, equivalent to the right-click on the file to be compressed (rarname)->winrar-> unzip 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; }}
Calling methods
Winrarcsharp win = new Winrarcsharp (); win. RAR ("f:\\aaa\\", "f:\\", "A.rar");//Compression (compressing "f:\\aaa\\" directory Files to "F:\\a.rar") win. UnRAR ("F:\\ Yo M.zip", "f:\\mm", "GG"); Unzip (extract "F:\\ Yo m.zip" to "F:\\mm\\gg" directory)
7z Compression Universal class:
Using system;using system.collections.generic;using system.text;using microsoft.win32;using System.Diagnostics; Using system.io;///<summary>///name:stone///DATETIME:2012/1/4 16:26:08///description:7z Decompression Management class///</ Summary>public class _7zrar{//7z.exe installation Address Private Const string _7zexe = @ "D:\Program Files (x86) \7-zip\7z.exe"; <summary>///use 7zExE for compression///</summary>/<param name= "_7zpath" > the folder (physical path) to be compressed < ;/param>//<param name= "FilePath" > Compressed storage directory (physical path) </param>//<returns>true or FALSE. Compression successfully returns true, and vice versa, false. </returns> public static bool Un7zrar (string _7zpath, String filePath) {BOOL flag = false; string cmd; ProcessStartInfo StartInfo; Process process; try {cmd = String.Format (@ "x {0}-o{1}-y", _7zpath, FilePath); StartInfo = new ProcessStartInfo (); StartInfo. FileName = _7zexe; StartinFo. Arguments = cmd; StartInfo. WindowStyle = Processwindowstyle.hidden; Process = new process (); Process. StartInfo = StartInfo; Process. Start (); Process. WaitForExit (); if (process. hasexited) {flag = true; } process. Close (); } catch (Exception e) {throw e; } return flag; }}
WinRAR (WINZIP) compression and decompression implementation (C # window platform)