本文的原理是藉助Windows平臺安裝的WinRAR(WinZip)實現C#程序的調用(注:WinRAR壓縮解壓WinZip一樣適用)。shell
先來看WinRAR(WinZip)自身的支持調用命令:orm
壓縮命令:a {0} {1} -r 【{0}:壓縮後文件名|{1}:待壓縮的文件物理路徑】blog
ex:"a 你妹.rar f:\\MM -r" (含義爲將f盤下MM的文件夾壓縮爲"你妹.rar"文件)進程
解壓命令:x {0} {1} -y 【{0}:待解壓文件名稱|{1}:待解壓文件物理路徑】ip
ex:"x 幺妹.rar f:\\幺妹 -y"(待壓縮文件物理路徑:"f:\\幺妹\\幺妹.rar")cmd
參數說明string
參數it |
含義io |
atable |
添加文件到壓縮包 |
x |
以完整路徑從壓縮包解開壓縮 |
WinZip(WinRAR)調用通用類
using System; using System.Collections.Generic; using System.Text; //--------------using using System.Diagnostics; using Microsoft.Win32; using System.IO; /// <summary> /// Name:Stone /// DateTime: 2011/12/31 16:39:26 /// Description:WinRAR壓縮 /// </summary> public class WinRARCSharp { // WinRAR安裝註冊表key private const string WinRAR_KEY = @"WinRAR.ZIP\shell\open\command"; /// <summary> /// 利用 WinRAR 進行壓縮 /// </summary> /// <param name="path">將要被壓縮的文件夾(絕對路徑)</param> /// <param name="rarPath">壓縮後的 .rar 的存放目錄(絕對路徑)</param> /// <param name="rarName">壓縮文件的名稱(包括後綴)</param> /// <returns>true 或 false。壓縮成功返回 true,反之,false。</returns> public bool RAR(string path, string rarPath, string rarName) { bool flag = false; string rarexe; //WinRAR.exe 的完整路徑 RegistryKey regkey; //註冊表鍵 Object regvalue; //鍵值 string cmd; //WinRAR 命令參數 ProcessStartInfo startinfo; Process process; try { regkey = Registry.ClassesRoot.OpenSubKey(WinRAR_KEY); regvalue = regkey.GetValue(""); // 鍵值爲 "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); //壓縮命令,至關於在要壓縮的文件夾(path)上點右鍵->WinRAR->添加到壓縮文件->輸入壓縮文件名(rarName) cmd = string.Format("a {0} {1} -r", rarName, path); startinfo = new ProcessStartInfo(); startinfo.FileName = rarexe; startinfo.Arguments = cmd; //設置命令參數 startinfo.WindowStyle = ProcessWindowStyle.Hidden; //隱藏 WinRAR 窗口 startinfo.WorkingDirectory = rarPath; process = new Process(); process.StartInfo = startinfo; process.Start(); process.WaitForExit(); //無限期等待進程 winrar.exe 退出 if (process.HasExited) { flag = true; } process.Close(); } catch (Exception e) { throw e; } return flag; } /// <summary> /// 利用 WinRAR 進行解壓縮 /// </summary> /// <param name="path">文件解壓路徑(絕對)</param> /// <param name="rarPath">將要解壓縮的 .rar 文件的存放目錄(絕對路徑)</param> /// <param name="rarName">將要解壓縮的 .rar 文件名(包括後綴)</param> /// <returns>true 或 false。解壓縮成功返回 true,反之,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); //解壓縮命令,至關於在要壓縮文件(rarName)上點右鍵->WinRAR->解壓到當前文件夾 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; } }
調用方法
WinRARCSharp win = new WinRARCSharp(); win.RAR("F:\\aaa\\", "f:\\", "a.rar"); // 壓縮(將「f:\\aaa\\」目錄文件壓縮到「f:\\a.rar」) win.UnRAR("f:\\呦M.zip", "f:\\MM", "GG"); // 解壓(將「f:\\呦M.zip」解壓到「f:\\MM\\GG」目錄下)
7z壓縮通用類:
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解壓管理類 /// </summary> public class _7zRAR { // 7z.exe 安裝地址 private const string _7zEXE = @"D:\Program Files (x86)\7-Zip\7z.exe"; /// <summary> /// 利用 7zExE 進行壓縮 /// </summary> /// <param name="_7zPath">將要被壓縮的文件夾(物理路徑)</param> /// <param name="filePath">壓縮後的的存放目錄(物理路徑)</param> /// <returns>true 或 false。壓縮成功返回 true,反之,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; } }