0. 判断是否安装WinRAR
public static bool ExistsRar(out String winRarPath)
{
winRarPath = String.Empty;
//通过Regedit(注册表)找到WinRar文件
var registryKey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\WinRAR.exe");
if (registryKey == null) return false;//未安装
//registryKey = theReg;可以直接返回Registry对象供会面操作
winRarPath = registryKey.GetValue("").ToString();
//这里为节约资源,直接返回路径,反正下面也没用到
registryKey.Close();//关闭注册表
return !String.IsNullOrEmpty(winRarPath);
}
1.压缩文件
a -afzip -m0 -ep1 -r “压缩后文件名” “待压缩文件所在路径”
WinRAR命令行
//[a] 添加到压缩文件
//afzip 执行zip压缩方式,方便用户在不同环境下使用。
//(取消该参数则执行rar压缩)
//-m0 存储 添加到压缩文件时不压缩文件。共6个级别【0-5】,值越大效果越好,也越慢
//ep1 依名称排除主目录(生成的压缩文件不会出现不必要的层级)
//r 修复压缩档案
//t 测试压缩档案内的文件
//as 同步压缩档案内容
//-p 给压缩文件加密码方式为:-p123456
/// <param name="foderPath">文件夹路径</param>
/// <param name="rarPath">生成压缩文件的路径</param>
/// <param name="rarFile">生成压缩文件的文件名</param>
public static void CompressRar(String foderPath, String rarFile)
{
try
{
String winRarPath = null;
//验证WinRar是否安装。
if (!ExistsRar(out winRarPath)) return;
//获得文件的Dir
String rarPath = Path.GetDirectoryName(rarFile);
//如果生成路径不存在则创建
if (!Directory.Exists(rarPath))
{
Directory.CreateDirectory(rarPath); }
var pathInfo = String.Format("a -afzip -ibck -m0 -ep1 -r \"{0}\" \"{1}\"", rarFile, foderPath);
#region WinRar 用到的命令注释
//[a] 添加到压缩文件
//afzip 执行zip压缩方式,方便用户在不同环境下使用。
//(取消该参数则执行rar压缩)
//-m0 存储 添加到压缩文件时不压缩文件。共6个级别【0-5】,值越大效果越好,也越慢
//ep1 依名称排除主目录(生成的压缩文件不会出现不必要的层级)
//r 修复压缩档案
//t 测试压缩档案内的文件
//as 同步压缩档案内容
//-p 给压缩文件加密码方式为:-p123456
#endregion
//打包文件存放目录
var process = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = winRarPath,//执行的文件名
Arguments = pathInfo,//需要执行的命令
UseShellExecute = false,//使用Shell执行
WindowStyle = ProcessWindowStyle.Hidden,//隐藏窗体
WorkingDirectory = rarPath,//rar 存放位置
CreateNoWindow = false,//不显示窗体
},
};
process.Start();//开始执行
process.WaitForExit();//等待完成并退出
process.Close();//关闭调用 cmd 的什么什么
}
catch (Exception ex)
{
throw ex;
}
}
解压文件
/// <param name="unRarPath">文件夹路径</param>
/// <param name="rarPath">压缩文件的路径</param>
/// <param name="rarName">压缩文件的文件名</param>
/// <returns></returns>
public static String UnCompressRar(String rarFile, String unRarPath)
{
string rarName = Path.GetFileNameWithoutExtension(rarFile);
string rarPath = Path.GetDirectoryName(rarFile);
try
{
String winRarPath = null;
//验证压缩文件是否存在
if (!ExistsRar(out winRarPath)) return "";
if (Directory.Exists(unRarPath) == false)
{
Directory.CreateDirectory(unRarPath);
}
var pathInfo = "x " + rarName + " " + unRarPath + " -y -ibck";
var process = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = winRarPath,//执行的文件名
Arguments = pathInfo,//需要执行的命令
UseShellExecute = false,//使用Shell执行
WindowStyle = ProcessWindowStyle.Hidden,//隐藏窗体
WorkingDirectory = rarPath,//rar 存放位置
CreateNoWindow = false,//不显示窗体
},
};
process.Start();//开始执行
process.WaitForExit();//等待完成并退出
process.Close();//关闭调用 cmd 的什么什么
}
catch (Exception ex)
{
throw ex;
}
return unRarPath;
}