using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using Microsoft.Win32;
using System.Diagnostics;
/******************************************************
程序用途:實現文件[文件夾]壓縮解壓功能函數
程序備註:
* 服務器端WinRAR支持
* 路徑簡述必須絕對路徑
******************************************************/
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string oFilePath = @"C:\1\2.txt";
string oRARFileName = "1.rar";
string oToFilePath = @"C:\1\";
//if (RAR(oFilePath, oRARFileName, oToFilePath))
if (RAR(oFilePath, oRARFileName, oToFilePath))
Response.Write("OK");
else
Response.Write("No");
//if (UnRAR(oFilePath, oRARFileName, oToFilePath))
// Response.Write("OK");
//else
// Response.Write("No");
}
/// <summary>
/// 壓縮文件
/// </summary>
/// <param name="DFilePath">需要壓縮的文件夾或者單個文件</param>
/// <param name="DRARName">生成壓縮文件的文件名</param>
/// <param name="DRARPath">生成壓縮文件保存路徑</param>
/// <returns></returns>
protected bool RAR(string DFilePath, string DRARName, string DRARPath)
{
String the_rar;
RegistryKey the_Reg;
Object the_Obj;
String the_Info;
ProcessStartInfo the_StartInfo;
Process the_Process;
try
{
the_Reg = Registry.ClassesRoot.OpenSubKey(@"Applications\WinRAR.exe\Shell\Open\Command");
the_Obj = the_Reg.GetValue("");
the_rar = the_Obj.ToString();
the_Reg.Close();
the_rar = the_rar.Substring(1, the_rar.Length - 7);
the_Info = " a " + " " + DRARName + " " + DFilePath; //命令 + 壓縮後文件名 + 被壓縮的文件或者路徑
the_StartInfo = new ProcessStartInfo();
the_StartInfo.FileName = the_rar;
the_StartInfo.Arguments = the_Info;
the_StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
the_StartInfo.WorkingDirectory = DRARPath; //RaR文件的存放目錄。
the_Process = new Process();
the_Process.StartInfo = the_StartInfo;
the_Process.Start();
return true;
}
catch (Exception ex)
{
return false;
}
}
/// <summary>
/// 解壓縮到指定文件夾
/// </summary>
/// <param name="RARFilePath">壓縮文件存在的目錄 </param>
/// <param name="RARFileName">壓縮文件名稱 </param>
/// <param name="UnRARFilePath">解壓到文件夾</param>
/// <returns></returns>
protected bool UnRAR(string RARFilePath, string RARFileName, string UnRARFilePath)
{
//解壓縮
String the_rar;
RegistryKey the_Reg;
Object the_Obj;
String the_Info;
ProcessStartInfo the_StartInfo;
Process the_Process;
try
{
the_Reg = Registry.ClassesRoot.OpenSubKey(@"Applications\WinRar.exe\Shell\Open\Command");
the_Obj = the_Reg.GetValue("");
the_rar = the_Obj.ToString();
the_Reg.Close();
the_rar = the_rar.Substring(1, the_rar.Length - 7);
the_Info = @" X " + " " + RARFilePath + RARFileName + " " + UnRARFilePath;
the_StartInfo = new ProcessStartInfo();
the_StartInfo.FileName = the_rar;
the_StartInfo.Arguments = the_Info;
the_StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
the_Process = new Process();
the_Process.StartInfo = the_StartInfo;
the_Process.Start();
return true;
}
catch (Exception ex)
{
return false;
}
}
}