Asp.net调用RAR压缩文件与解压文件源码

Asp.net调用RAR压缩文件与解压文件源码

源代码
+展开
-C#
//压缩
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 最好 使用最大压缩方式(最好的压缩,但是最慢)

文章来自学IT网:http://www.xueit.com/html/2009-03/21_827_00.html

加支付宝好友偷能量挖...


评论(0)网络
阅读(76)喜欢(0)Asp.Net/C#/WCF