using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Win32;
using System.IO;
using System.Diagnostics;
namespace JaveSystem
{
public class WinRar
{
static WinRar()
{
//判斷是否安裝了WinRar.exe
RegistryKey key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\WinRAR.exe");
_existSetupWinRar = !string.IsNullOrEmpty(key.GetValue(string.Empty).ToString());
//擷取WinRar.exe路徑
_winRarPath = key.GetValue(string.Empty).ToString();
}
static bool _existSetupWinRar;
/// <summary>
/// 擷取是否安裝了WinRar的辨別
/// </summary>
public static bool ExistSetupWinRar
get { return _existSetupWinRar; }
static string _winRarPath;
/// 擷取WinRar.exe路徑
public static string WinRarPath
get { return _winRarPath; }
/// 壓縮到.rar
/// <param name="intputPath">輸入目錄</param>
/// <param name="outputPath">輸出目錄</param>
/// <param name="outputFileName">輸出檔案名</param>
public static void CompressRar(string intputPath, string outputPath, string outputFileName)
//rar 執行時的指令、參數
string rarCmd;
//啟動程序的參數
ProcessStartInfo processStartInfo;
//程序對象
Process process;
try
{
if (!ExistSetupWinRar)
{
throw new ArgumentException("Not setuping the winRar, you can Compress.make sure setuped winRar.");
}
//判斷輸入目錄是否存在
if (!Directory.Exists(intputPath))
throw new ArgumentException("CompressRar'arge : inputPath isn't exsit.");
//指令參數
rarCmd = " a " + outputFileName + " " + intputPath + " -r";
//建立啟動程序的參數
processStartInfo = new ProcessStartInfo();
//指定啟動檔案名
processStartInfo.FileName = WinRarPath;
//指定啟動該檔案時的指令、參數
processStartInfo.Arguments = rarCmd;
//指定啟動視窗模式:隐藏
processStartInfo.WindowStyle = ProcessWindowStyle.Hidden;
//指定壓縮後到達路徑
processStartInfo.WorkingDirectory = outputPath;
//建立程序對象
process = new Process();
//指定程序對象啟動資訊對象
process.StartInfo = processStartInfo;
//啟動程序
process.Start();
//指定程序自行退行為止
process.WaitForExit();
}
catch (Exception ex)
throw ex;
/// 解壓.rar
/// <param name="inputRarFileName">輸入.rar</param>
public static void UnCompressRar(string inputRarFileName, string outputPath)
throw new ArgumentException("Not setuping the winRar, you can UnCompress.make sure setuped winRar.");
//如果壓縮到目标路徑不存在
if (!Directory.Exists(outputPath))
//建立壓縮到目标路徑
Directory.CreateDirectory(outputPath);
rarCmd = "x " + inputRarFileName + " " + outputPath + " -y";
}
}
RAR參數:
一、壓縮指令
1、将temp.txt壓縮為temp.rarrar a temp.rar temp.txt
2、将目前目錄下所有檔案壓縮到temp.rarrar a temp.rar *.*
3、将目前目錄下所有檔案及其所有子目錄壓縮到temp.rarrar a temp.rar *.* -r
4、将目前目錄下所有檔案及其所有子目錄壓縮到temp.rar,并加上密碼123rar a temp.rar *.* -r -p123
二、解壓指令
1、将temp.rar解壓到c:\temp目錄rar e temp.rar c:\temprar e *.rar c:\temp(支援批量操作)
2、将temp.rar解壓到c:\temp目錄,并且解壓後的目錄結構和temp.rar中的目錄結構一
壓縮目錄test及其子目錄的檔案内容
Wzzip test.zip test -r -P
WINRAR A test.rar test -r
删除壓縮包中的*.txt檔案
Wzzip test.zip *.txt -d
WinRAR d test.rar *.txt
重新整理壓縮包中的檔案,即添加已經存在于壓縮包中但更新的檔案
Wzzip test.zip test -f
Winrar f test.rar test
更新壓縮包中的檔案,即添加已經存在于壓縮包中但更新的檔案以及新檔案
Wzzip test.zip test -u
Winrar u test.rar test
移動檔案到壓縮包,即添加檔案到壓縮包後再删除被壓縮的檔案
Wzzip test.zip -r -P -m
Winrar m test.rar test -r
添加全部 *.exe 檔案到壓縮檔案,但排除有 a或b
開頭名稱的檔案
Wzzip test *.exe -xf*.* -xb*.*
WinRAR a test *.exe -xf*.* -xb*.*
加密碼進行壓縮
Wzzip test.zip test
-s123。注意密碼是大小寫敏感的。在圖形界面下打開帶密碼的壓縮檔案,會看到+号标記(附圖1)。
WINRAR A test.rar test -p123
-r。注意密碼是大小寫敏感的。在圖形界面下打開帶密碼的壓縮檔案,會看到*号标記(附圖2)。
按名字排序、以簡要方式清單顯示壓縮封包件
Wzzip test.zip -vbn
Rar l test.rar
鎖定壓縮包,即防止未來對壓縮包的任何修改
無對應指令
Winrar k test.rar
建立360kb大小的分卷壓縮包
Winrar a -v360 test
帶子目錄資訊解壓縮檔案
Wzunzip test -d
Winrar x test -r
不帶子目錄資訊解壓縮檔案
Wzunzip test
Winrar e test
解壓縮檔案到指定目錄,如果目錄不存在,自動建立
Wzunzip test newfolder
Winrar x test newfolder
解壓縮檔案并确認覆寫檔案
Wzunzip test -y
Winrar x test -y
解壓縮特定檔案
Wzunzip test *.txt
Winrar x test *.txt
解壓縮現有檔案的更新檔案
Wzunzip test -f
Winrar x test -f
解壓縮現有檔案的更新檔案及新檔案
Wzunzip test -n
Winrar x test -u
批量解壓縮檔案
Wzunzip *.zip
WinRAR e *.rar