public static void DecompressRar(string outputPath, string inputPath, string inputFileName)
{
//rar 执行时的命令、参数
string rarCmd;
//启动进程的参数
ProcessStartInfo processStartInfo;
//进程对象
Process process;
//rar路径
string rarFile =System.Windows.Forms.Application.StartupPath + @"rar.exe" ;
//命令参数
rarCmd = " e " + inputFileName + " " + outputPath + " -r -ep1";
if (outputPath.IndexOf(' ') > 0 || inputPath.IndexOf(' ') > 0)
{
rarCmd = "x -inul -y -o+ -ep1 "" + inputPath + "" + inputFileName + "" "" + outputPath+""";
}
if (!File.Exists(System.Windows.Forms.Application.StartupPath + @"rar.exe"))
{
rarFile=@"C:Program FilesWinRARWinRAR.exe";
}
try
{
//创建启动进程的参数
processStartInfo = new ProcessStartInfo();
//指定启动文件名
processStartInfo.FileName = rarFile;
//指定启动该文件时的命令、参数
processStartInfo.Arguments = rarCmd;
//指定启动窗口模式:隐藏
processStartInfo.WindowStyle = ProcessWindowStyle.Hidden;
//指定解压后到达路径(文件夹需要存在)
processStartInfo.WorkingDirectory = inputPath;
//创建进程对象
process = new Process();
//指定进程对象启动信息对象
process.StartInfo = processStartInfo;
//启动进程
process.Start();
//指定进程自行退行为止
process.WaitForExit();
//释放资源
process.Close();
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
}
解压:
class UseWinRar
{
private string rarExeFile = null;//WinRar.exe路径
private bool useAble = false;//标志WinRar是否可用
public UseWinRar()//构造方法
{
rarExeFile = getRarExe();
useAble = !string.IsNullOrEmpty(rarExeFile);//如果WinRar.exe路径不为空,说明可用
}
public static string getRarExe()//获取WinRar所在磁盘路径
{
string rarExe = null;
RegistryKey regKey = Registry.LocalMachine.OpenSubKey(@"SOFTWAREMicrosoftWindowsCurrentVersionApp PathsWinRAR.exe");
if (regKey == null)
{
return null;
}
rarExe = regKey.GetValue("").ToString();
regKey.Close();//关闭注册表
return rarExe;
}
public bool exeRarCmd(string cmd)//执行某个命令
{
if (!useAble)
{
return false;
}
Process process = new Process();//新建一个过程
ProcessStartInfo startInfo = new ProcessStartInfo(rarExeFile);//新建一个启动信息
startInfo.Arguments = cmd;//设置启动信息的执行参数
//startInfo.WorkingDirectory = workDirectory;//设置启动信息的工作目录
startInfo.WindowStyle = ProcessWindowStyle.Hidden;//设置程序后台运行
process.StartInfo = startInfo;//设置过程的启动信息
process.Start();//开始过程
return true;
}
public bool unZipAll(string zipFile, string targetDirectory)//将指定压缩文件解压到指定目录
{
if (! File.Exists(zipFile))
{
return false;
}
string zipCmd = "x " + zipFile +" "+ targetDirectory + " -y -ibck";//后台解压压缩文件中全部文件到指定目录
exeRarCmd(zipCmd);//执行解压操作
return true;
}
public bool unZipToCurrentDirectory(string zipFile)//将压缩文件解压到当前目录
{
if (!File.Exists(zipFile))
{
return false;
}
FileInfo fileInfo = new FileInfo(zipFile);
return unZipAll(zipFile, fileInfo.DirectoryName);
}
}
Main:
public static void Main()
{
UseWinRar rar = new UseWinRar();
string[] zipFiles = Directory.GetFiles(Environment.CurrentDirectory, "*.zip");//获取所有zip文件路径
foreach (string zipFile in zipFiles)
{
rar.unZipToCurrentDirectory(zipFile);
}
}










