c#使用winrar实现压缩和解压缩
主要用于软件更新的时候还有内部文档的处理,主机和客户端的数据传输,稍后会提供7z的压缩文档
View Code
///=======================================
/// Copyright (c) 三月软件小组 2006 -2011
/// All right reserved
///
/// fileType:class
/// Description winrar压缩和解压缩
///
/// create Renji by at 2011-05-04
/// QQ:625246906
///============================================
using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
namespace winrar
{
public class winrar
{
/// 将一个文件从指定压缩文件中解压到指定的目录中
/// <summary>
/// 将一个文件从指定压缩文件中解压到指定的目录中
/// </summary>
/// <param name="SourcePath">压缩文件</param>
/// <param name="path">解压文件的目录</param>
/// <param name="fileName">要解压文件的名称</param>
/// <returns>return成功 否则 失败</returns>
public bool WinUnZipFile(string SourcePath ,string path,string fileName)
{
try
{
string arguments = "x " + SourcePath + " " + fileName + " " + path;
return WinrarProcess(arguments);
}
catch
{
return false;
}
}
/// 将一个文件放到压缩文件中
/// <summary>
/// 将一个文件放到压缩文件中
/// </summary>
/// <param name="SourcePath">压缩文件的路径及名字</param>
/// <param name="path">压缩 文件的路径</param>
/// <param name="fileName">压缩文件的名字</param>
/// <returns>return成功 否则 失败</returns>
public bool WinZipFile(string SourcePath, string path, string fileName)
{
try
{
System.IO.File.Exists(path + fileName);
string arguments = "a -ep " + SourcePath + " " + path + fileName;
return WinrarProcess(arguments );
}
catch
{
return false;
}
}
/// <summary>
/// 讲一个文件放到压缩文件中,并删除该文件
/// </summary>
/// <param name="SourcePath">压缩文件的路径及名字</param>
/// <param name="path">压缩 文件的路径</param>
/// <param name="fileName">压缩文件的名字</param>
/// <returns>return成功 否则 失败</returns>
public bool WinZipFileDel(string SourcePath, string path, string fileName)
{
try
{
System.IO.File.Exists(path + fileName);
string arguments = "a -ep -df " + SourcePath + " " + path + fileName;
return WinrarProcess(arguments);
}
catch
{
return false;
}
}
/// 将一个rar文件解压到指定的位置,,不带压缩目录
/// <summary>
/// 将一个rar文件解压到指定的位置,,不带压缩目录
/// </summary>
/// <param name="rarPath">rar压缩文件的路径</param>
/// <param name="UnZippath">解压文件要放的路径</param>
/// <returns>return成功 否则 失败</returns>
public bool WinUnZip(string rarPath, string UnZippath)
{
try
{
string arguments = string.Format ("e \"{0}\" \"{1}\" ",rarPath ,UnZippath );
return WinrarProcess(arguments );
}
catch
{
return false;
}
}
/// 将一个rar文件解压到指定的位置,不带压缩目录
/// <summary>
/// 将一个rar文件解压到指定的位置
/// </summary>
/// <param name="rarPath">rar压缩文件的路径</param>
/// <param name="UnZippath">解压文件要放的路径</param>
/// <returns>return成功 否则 失败</returns>
public bool WinUnZip(string rarPath, string UnZippath,int a)
{
try
{
string arguments = string.Format("x \"{0}\" \"{1}\" ", rarPath, UnZippath);
return WinrarProcess(arguments);
}
catch
{
return false;
}
}
/// 带目录进行压缩
/// <summary>
/// 带目录进行压缩
/// </summary>
/// <param name="raPath">压缩文件的路径</param>
/// <param name="filePath">被压缩文件夹的路径</param>
/// <returns>return成功 否则 失败</returns>
public bool WinZipWithFile(string raPath,string filePath)
{
try
{
string arguments = string.Format("a \"{0}\" \"{1}\" ", raPath, filePath);
return WinrarProcess(arguments);
}
catch
{
return false;
}
}
/// 不带目录进行压缩
/// <summary>
/// 不带目录进行压缩
/// </summary>
/// <param name="raPath">压缩文件的路径</param>
/// <param name="filePath">被压缩文件夹的路径</param>
/// <returns>return成功 否则 失败</returns>
public bool WinZipNotFile(string raPath, string filePath)
{
try
{
string arguments = string.Format("a -ep \"{0}\" \"{1}\" ", raPath, filePath);
return WinrarProcess(arguments);
}
catch
{
return false;
}
}
/// winrar 的执行程序
/// <summary>
/// winrar 的执行程序
/// </summary>
/// <param name="arguments">要执行的winrar命令</param>
/// <returns> return成功 否则 失败</returns>
private bool WinrarProcess(string arguments)
{
try
{
Process winrar = new Process();
winrar.StartInfo.FileName = "winrar.exe";
winrar.StartInfo.CreateNoWindow = true;
winrar.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
winrar.StartInfo.WindowStyle = ProcessWindowStyle.Minimized;
winrar.StartInfo.Arguments = arguments;
winrar.Start();
winrar.WaitForExit();
if (winrar.HasExited)
{
int iExitCode = winrar.ExitCode;
if (iExitCode == 0)
{
//正常完成
return true;
}
else
{
//有错
return false;
}
}
winrar.Close();
return false;
}
catch
{
return false;
}
}
}
}
/// Copyright (c) 三月软件小组 2006 -2011
/// All right reserved
///
/// fileType:class
/// Description winrar压缩和解压缩
///
/// create Renji by at 2011-05-04
/// QQ:625246906
///============================================
using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
namespace winrar
{
public class winrar
{
/// 将一个文件从指定压缩文件中解压到指定的目录中
/// <summary>
/// 将一个文件从指定压缩文件中解压到指定的目录中
/// </summary>
/// <param name="SourcePath">压缩文件</param>
/// <param name="path">解压文件的目录</param>
/// <param name="fileName">要解压文件的名称</param>
/// <returns>return成功 否则 失败</returns>
public bool WinUnZipFile(string SourcePath ,string path,string fileName)
{
try
{
string arguments = "x " + SourcePath + " " + fileName + " " + path;
return WinrarProcess(arguments);
}
catch
{
return false;
}
}
/// 将一个文件放到压缩文件中
/// <summary>
/// 将一个文件放到压缩文件中
/// </summary>
/// <param name="SourcePath">压缩文件的路径及名字</param>
/// <param name="path">压缩 文件的路径</param>
/// <param name="fileName">压缩文件的名字</param>
/// <returns>return成功 否则 失败</returns>
public bool WinZipFile(string SourcePath, string path, string fileName)
{
try
{
System.IO.File.Exists(path + fileName);
string arguments = "a -ep " + SourcePath + " " + path + fileName;
return WinrarProcess(arguments );
}
catch
{
return false;
}
}
/// <summary>
/// 讲一个文件放到压缩文件中,并删除该文件
/// </summary>
/// <param name="SourcePath">压缩文件的路径及名字</param>
/// <param name="path">压缩 文件的路径</param>
/// <param name="fileName">压缩文件的名字</param>
/// <returns>return成功 否则 失败</returns>
public bool WinZipFileDel(string SourcePath, string path, string fileName)
{
try
{
System.IO.File.Exists(path + fileName);
string arguments = "a -ep -df " + SourcePath + " " + path + fileName;
return WinrarProcess(arguments);
}
catch
{
return false;
}
}
/// 将一个rar文件解压到指定的位置,,不带压缩目录
/// <summary>
/// 将一个rar文件解压到指定的位置,,不带压缩目录
/// </summary>
/// <param name="rarPath">rar压缩文件的路径</param>
/// <param name="UnZippath">解压文件要放的路径</param>
/// <returns>return成功 否则 失败</returns>
public bool WinUnZip(string rarPath, string UnZippath)
{
try
{
string arguments = string.Format ("e \"{0}\" \"{1}\" ",rarPath ,UnZippath );
return WinrarProcess(arguments );
}
catch
{
return false;
}
}
/// 将一个rar文件解压到指定的位置,不带压缩目录
/// <summary>
/// 将一个rar文件解压到指定的位置
/// </summary>
/// <param name="rarPath">rar压缩文件的路径</param>
/// <param name="UnZippath">解压文件要放的路径</param>
/// <returns>return成功 否则 失败</returns>
public bool WinUnZip(string rarPath, string UnZippath,int a)
{
try
{
string arguments = string.Format("x \"{0}\" \"{1}\" ", rarPath, UnZippath);
return WinrarProcess(arguments);
}
catch
{
return false;
}
}
/// 带目录进行压缩
/// <summary>
/// 带目录进行压缩
/// </summary>
/// <param name="raPath">压缩文件的路径</param>
/// <param name="filePath">被压缩文件夹的路径</param>
/// <returns>return成功 否则 失败</returns>
public bool WinZipWithFile(string raPath,string filePath)
{
try
{
string arguments = string.Format("a \"{0}\" \"{1}\" ", raPath, filePath);
return WinrarProcess(arguments);
}
catch
{
return false;
}
}
/// 不带目录进行压缩
/// <summary>
/// 不带目录进行压缩
/// </summary>
/// <param name="raPath">压缩文件的路径</param>
/// <param name="filePath">被压缩文件夹的路径</param>
/// <returns>return成功 否则 失败</returns>
public bool WinZipNotFile(string raPath, string filePath)
{
try
{
string arguments = string.Format("a -ep \"{0}\" \"{1}\" ", raPath, filePath);
return WinrarProcess(arguments);
}
catch
{
return false;
}
}
/// winrar 的执行程序
/// <summary>
/// winrar 的执行程序
/// </summary>
/// <param name="arguments">要执行的winrar命令</param>
/// <returns> return成功 否则 失败</returns>
private bool WinrarProcess(string arguments)
{
try
{
Process winrar = new Process();
winrar.StartInfo.FileName = "winrar.exe";
winrar.StartInfo.CreateNoWindow = true;
winrar.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
winrar.StartInfo.WindowStyle = ProcessWindowStyle.Minimized;
winrar.StartInfo.Arguments = arguments;
winrar.Start();
winrar.WaitForExit();
if (winrar.HasExited)
{
int iExitCode = winrar.ExitCode;
if (iExitCode == 0)
{
//正常完成
return true;
}
else
{
//有错
return false;
}
}
winrar.Close();
return false;
}
catch
{
return false;
}
}
}
}
大家有什么还要实现的功能可以留言?或者提供新的功能