调用winrar压缩解压缩文件
public class WinRARManager
{
/// <summary>
/// 是否安装了Winrar
/// </summary>
/// <returns></returns>
static public bool Exists()
{
bool haveWinRAR = true;
RegistryKey the_Reg = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\WinRAR.exe");
if (the_Reg == null)
{
haveWinRAR = false;
}
else
{
if (string.IsNullOrEmpty(the_Reg.GetValue("").ToString()))
{
haveWinRAR = false;
}
}
return haveWinRAR;
}
/// <summary>
/// 路径加引号(当路径中有空格时会导致调用WinRAR压缩或解压函数失败,给路径加上引号可以解决。)
/// </summary>
/// <param name="path">路径</param>
/// <returns>加上引号后的路径</returns>
private static string GetPath(string path)
{
return "\"" + path + "\"";
}
/// <summary>
/// 压缩
/// </summary>
/// <param name="filePath">要压缩的文件所在目录或要压缩的文件名(带路径)</param>
/// <param name="rarPath">压缩文件文件名(含路径)</param>
/// <param name="isSavePath">是否存储文件路径</param>
/// <returns>压缩是否成功</returns>
public static bool CompressRAR(string filePath, string rarPath, bool isSavePath)
{
bool success = false;
string the_rar;
RegistryKey the_Reg;
object the_Obj;
string the_Info;
Process the_Process = new Process();
string cmd = "";
try
{
the_Reg = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\WinRAR.exe");
the_Obj = the_Reg.GetValue("");
the_rar = the_Obj.ToString();
the_Reg.Close();
string directory = rarPath.Substring(0, rarPath.LastIndexOf("\\"));
if (!string.IsNullOrEmpty(directory))
{
if (!Directory.Exists(directory))
{
Directory.CreateDirectory(rarPath);
}
}
filePath = GetPath(filePath);
rarPath = GetPath(rarPath);
//命令参数
//the_Info = " a " + rarName + " " + @"C:Test?70821.txt"; //文件压缩
if (isSavePath)
{
cmd = "a -r ";
}
else
{
cmd = "a -ep ";
}
the_Info = cmd + rarPath + " " + filePath;
//打包文件存放目录
//the_StartInfo.WorkingDirectory = rarPath;
the_Process.StartInfo.FileName = the_rar;
the_Process.StartInfo.Arguments = the_Info;
the_Process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
the_Process.Start();
the_Process.WaitForExit();
if (the_Process.HasExited)
{
int iExitCode = the_Process.ExitCode;
if (iExitCode == 0)
{
//正常完成
Console.WriteLine("成功");
success = true;
}
else
{
//有错
Console.WriteLine("失败");
success = false;
}
}
}
catch
{
success = false;
}
finally
{
the_Process.Close();
}
return success;
}
/// <summary>
/// 压缩
/// </summary>
/// <param name="filePath">要压缩的文件所在目录</param>
/// <param name="rarPath">压缩文件存放目录</param>
/// <param name="rarName">压缩文件名(不含路径)</param>
/// <returns>压缩是否成功</returns>
private bool CompressRAR(string filePath, string rarPath, string rarName)
{
bool success = false;
string the_rar;
RegistryKey the_Reg;
object the_Obj;
string the_Info;
ProcessStartInfo the_StartInfo;
Process the_Process;
try
{
the_Reg = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\WinRAR.exe");
the_Obj = the_Reg.GetValue("");
the_rar = the_Obj.ToString();
the_Reg.Close();
//the_rar = the_rar.Substring(1, the_rar.Length - 7);
if (!Directory.Exists(rarPath))
{
Directory.CreateDirectory(rarPath);
}
filePath = GetPath(filePath);
rarPath = GetPath(rarPath);
//命令参数
//the_Info = " a " + rarName + " " + @"C:Test?70821.txt"; //文件压缩
the_Info = "a -r" + rarName + " " + filePath;
the_StartInfo = new ProcessStartInfo();
//the_StartInfo.FileName = the_rar;
the_StartInfo.FileName = the_rar;
the_StartInfo.Arguments = the_Info;
the_StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
//打包文件存放目录
the_StartInfo.WorkingDirectory = rarPath;
the_Process = new Process();
the_Process.StartInfo = the_StartInfo;
the_Process.Start();
the_Process.WaitForExit();
if (the_Process.HasExited)
{
int iExitCode = the_Process.ExitCode;
if (iExitCode == 0)
{
//正常完成
Console.WriteLine("成功");
success = true;
}
else
{
//有错
Console.WriteLine("失败");
success = false;
}
}
the_Process.Close();
}
catch
{
success = false;
}
return success;
}
/// <summary>
/// 解压
/// </summary>
/// <param name="unRarPath">解压后文件存放目录</param>
/// <param name="rarPath">要解压的压缩文件名(含路径)</param>
/// <param name="includeUnCompressPath">解压时是否包含路径</param>
/// <param name="isTip">覆盖文件时是否提示</param>
/// <returns>解压是否成功</returns>
public static bool UnCompressRAR(string unRarPath, string rarPath, bool includeUnCompressPath, bool isTip)
{
bool success = false;
string the_rar;
RegistryKey the_Reg;
object the_Obj;
string the_Info;
Process the_Process = new Process();
string cmd = "";
try
{
the_Reg = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\WinRAR.exe");
the_Obj = the_Reg.GetValue("");
the_rar = the_Obj.ToString();
the_Reg.Close();
if (!Directory.Exists(unRarPath))
{
Directory.CreateDirectory(unRarPath);
}
unRarPath = GetPath(unRarPath);
rarPath = GetPath(rarPath);
if (includeUnCompressPath)
{
if (isTip)
{
cmd = "x ";
}
else
{
cmd = "x -y ";
}
}
else
{
if (isTip)
{
cmd = "e ";
}
else
{
cmd = "e -y ";
}
}
the_Info = cmd + rarPath + " " + unRarPath;
//the_StartInfo.WorkingDirectory = rarPath;//获取压缩包路径
the_Process.StartInfo.FileName = the_rar;
the_Process.StartInfo.Arguments = the_Info;
the_Process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
the_Process.Start();
the_Process.WaitForExit();
if (the_Process.HasExited)
{
int iExitCode = the_Process.ExitCode;
if (iExitCode == 0)
{
//正常完成
Console.WriteLine("成功");
success = true;
}
else
{
//有错
Console.WriteLine("失败");
success = false;
}
}
}
catch
{
success = false;
}
finally
{
the_Process.Close();
}
return success;
}
/// <summary>
/// 解压
/// </summary>
/// <param name="unRarPath">解压后文件存放目录</param>
/// <param name="rarPath">要解压的压缩文件所在目录</param>
/// <param name="rarName">压缩文件名</param>
/// <returns>解压是否成功</returns>
private bool UnCompressRAR(string unRarPath, string rarPath, string rarName)
{
bool success = false;
string the_rar;
RegistryKey the_Reg;
object the_Obj;
string the_Info;
try
{
the_Reg = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\WinRAR.exe");
the_Obj = the_Reg.GetValue("");
the_rar = the_Obj.ToString();
the_Reg.Close();
//the_rar = the_rar.Substring(1, the_rar.Length - 7);
if (Directory.Exists(unRarPath) == false)
{
Directory.CreateDirectory(unRarPath);
}
unRarPath = GetPath(unRarPath);
rarPath = GetPath(rarPath);
the_Info = "x -y" + rarName + " " + unRarPath;
ProcessStartInfo the_StartInfo = new ProcessStartInfo();
the_StartInfo.FileName = the_rar;
the_StartInfo.Arguments = the_Info;
the_StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
the_StartInfo.WorkingDirectory = rarPath;//获取压缩包路径
Process the_Process = new Process();
the_Process.StartInfo = the_StartInfo;
the_Process.Start();
the_Process.WaitForExit();
if (the_Process.HasExited)
{
int iExitCode = the_Process.ExitCode;
if (iExitCode == 0)
{
//正常完成
Console.WriteLine("成功");
success = true;
}
else
{
//有错
Console.WriteLine("失败");
success = false;
}
}
the_Process.Close();
}
catch
{
success = false;
}
return success;
}
}
posted on 2011-11-04 11:39 lijinchang 阅读(416) 评论(0) 编辑 收藏 举报