protected void btnZip_Click(object sender, EventArgs e)
{
//string filePath1 = Server.MapPath("~/ZWB");//取路径
string filePath1 = Path.GetDirectoryName(@"E:\ZWB\"); //取路径
string filePath = filePath1 + @"\" + System.IO.Path.GetFileNameWithoutExtension(filePath1); //取压缩文件的名字
if (filePath1 != string.Empty)
{
//压缩
String myRar;
RegistryKey myReg;
Object myObj;
String myInfo;
ProcessStartInfo myStartInfo;
Process myProcess;
try
{
myReg = Registry.ClassesRoot.OpenSubKey(@"Applications\WinRAR.exe\Shell\Open\Command");
myObj = myReg.GetValue("");
myRar = myObj.ToString();
myReg.Close();
myRar = myRar.Substring(1, myRar.Length - 7);
myInfo = "a -r -ep1 " + filePath + ".zip " + filePath1;
//1、这里是压缩整个目录
// 2、“XXX.zip” 是压缩后的文件名称
// 3、lbTree.Text + @"zip"是需要压缩的文件或文件夹,可多个
myStartInfo = new ProcessStartInfo();
myStartInfo.FileName = myRar;
myStartInfo.Arguments = myInfo;
myStartInfo.WindowStyle = ProcessWindowStyle.Hidden;
myStartInfo.WorkingDirectory = filePath1 + "zip"; //获取或设置要启动的进程的初始目录,即压缩后的文件的路径。
myProcess = new Process();
myProcess.StartInfo = myStartInfo;
myProcess.Start();
Response.Write("<font color=red>压缩成功</font>");
}
catch (Exception ex)
{
Response.Write(ex.ToString());
}
}
else
{
Response.Write("<font color=red>压缩文件不存在请上传要压缩的文件</font>");
}
}