使用WinRar压缩文件或文件夹并下载
2010-09-01 11:05
239 查看
using Microsoft.Win32;
using System.Diagnostics;
protected void Button1_Click(object sender, EventArgs e)
{
string folder = Server.MapPath("images");
RAR(@folder, "026", @folder);
Response.Redirect("images/026.rar");
}
/// <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 +" -ep1"; //命令 + 压缩后文件名 + 被压缩的文件或者路径
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();
the_Process.WaitForExit();
the_Process.Close();
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;
}
}
using System.Diagnostics;
protected void Button1_Click(object sender, EventArgs e)
{
string folder = Server.MapPath("images");
RAR(@folder, "026", @folder);
Response.Redirect("images/026.rar");
}
/// <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 +" -ep1"; //命令 + 压缩后文件名 + 被压缩的文件或者路径
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();
the_Process.WaitForExit();
the_Process.Close();
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;
}
}
相关文章推荐
- 使用计划任务将文件夹备份为压缩文件(使用WinRAR)
- 使用java对文件或文件夹进行压缩和加密
- C# 利用WinRAR (加密)压缩及解压缩 相关文件夹及文件
- java web 实现把文件夹下的所有文件压缩成zip并实现下载(原文链接)http://blog.csdn.net/sxdtzhaoxinguo/article/details/20550537)
- 将文件中的文件以及文件夹压缩,并下载
- C# 利用WinRAR (加密)压缩及解压缩 相关文件夹及文件
- asp.net实现文件夹及文件压缩,并实现下载(二)——文件超过150M
- QTP通过Winrar或者纯VBS代码压缩指定的文件或文件夹
- linux下使用ftp命令下载压缩文件,解压时出错的问题
- 使用zip对文件或文件夹进行压缩, 解压缩
- 使用Java zip包和apach 的ant包对文件、文件夹进行压缩,兼容中文
- 【Python】使用Python压缩文件/文件夹
- .net压缩文件夹及下载压缩文件
- php使用pclzip类实现文件压缩的方法(附pclzip类下载地址)
- 使用ruby脚本压缩文件夹(可排除文件或目录)
- 转:PHP 使用ZipArchive压缩文件并下载
- php使用pclzip类实现文件压缩的方法(附pclzip类下载地址)
- 使用URL下载指定的文件保存到指定的文件夹中。
- 使用java向FTP上传或下载压缩文件时的问题
- C#中使用WinRAR实现加密压缩及解压缩文件