標籤:
本文的原理是藉助Windows平台安裝的WinRAR(WinZip)實現C#程式的調用(註:WinRAR壓縮解壓WinZip同樣適用)。
先來看WinRAR(WinZip)自身的支援調用命令:
壓縮命令:a {0} {1} -r 【{0}:壓縮後檔案名稱|{1}:待壓縮的檔案實體路徑】
ex:"a 你妹.rar f:\\MM -r" (含義為將f盤下MM的檔案夾壓縮為"你妹.rar"檔案)
解壓命令:x {0} {1} -y 【{0}:待解壓檔案名稱|{1}:待解壓檔案實體路徑】
ex:"x 么妹.rar f:\\么妹 -y"(待壓縮檔實體路徑:"f:\\么妹\\么妹.rar")
參數說明
參數 |
含義 |
a |
添加檔案到壓縮包 |
x |
以完整路徑從壓縮包解開壓縮 |
WinZip(WinRAR)調用通用類
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壓縮/// </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; }}
WinRAR(WinZip)壓縮與解壓實現(C#版Window平台)