//壓縮
protected void btnY_Click(object sender, EventArgs e)
{
string rar;
RegistryKey reg;
string args;
ProcessStartInfo procStart;
Process process;
try
{
reg = Registry.ClassesRoot.OpenSubKey(@"Applications\WinRAR.exe\Shell\Open\Command");
rar = reg.GetValue("").ToString();//擷取註冊表rar安裝路徑
reg.Close();
rar = rar.Substring(1, rar.Length - 7);//擷取rar安裝路徑
args = "a -inul -y G:\\temp.rar G:\\1.txt";//這裡為rar的壓縮命令格式(也可以自行擴充)
procStart = new ProcessStartInfo();
procStart.FileName = rar;
procStart.Arguments = args;//參數
procStart.WindowStyle = ProcessWindowStyle.Hidden;//視窗狀態
procStart.WorkingDirectory = Server.MapPath(""); ;//擷取或設定要啟動的進程的初始目錄。
process = new Process();
process.StartInfo = procStart;
process.Start();
Response.Write("<script>alert('壓縮成功')</script>");
}
catch (Exception ex)
{
Response.Write(ex.ToString());
}
}
//解壓
protected void btnJ_Click(object sender, EventArgs e)
{
string rar;
RegistryKey reg;
string args;
ProcessStartInfo startInfo;
Process process;
try
{
reg = Registry.ClassesRoot.OpenSubKey(@"Applications\WinRar.exe\Shell\Open\Command");
rar = reg.GetValue("").ToString();
reg.Close();
rar = rar.Substring(1, rar.Length - 7);
args = " X E:\\temp.rar E:\\";
startInfo = new ProcessStartInfo();
startInfo.FileName = rar;
startInfo.Arguments = args;
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
process = new Process();
process.StartInfo = startInfo;
process.Start();
Response.Write("<script>alert('解壓成功')</script>");
}
catch (Exception ex)
{
Response.Write(ex.ToString());
}
}
例:把E:\web\目錄下的所有東西打包為1.rar放到E:\web\目錄下,以便下載
"C:\Program Files\WinRAR\Rar.exe" a -k -r -s -m1 E:\web\1.rar E:\web\
參數說明:
a 添加檔案到壓縮檔中
-k 鎖定壓縮檔
-s產生固體存檔,這樣可以增大壓縮比
-r包括子目錄
-m1 設定壓縮比
-m0 儲存 添加到壓縮檔時不壓縮檔。
-m1 最快 使用最快方式(低壓縮)
-m2 較快 使用快速壓縮方式
-m3 標準 使用標準(預設)壓縮方式
-m4 較好 使用較好壓縮方式(較好壓縮,但是慢)
-m5 最好 使用最大壓縮方式(最好的壓縮,但是最慢)