winrar 压缩
using System; using System.Collections.Generic; using System.Linq; using System.Text; using Microsoft.Win32; using System.Diagnostics; using System.IO; namespace TaiLian.Common { public class WinRARCSharp { // WinRAR安装注册表key private const string WinRAR_KEY = @"WinRAR.ZIP\shell\open\command"; /// <summary> /// /// </summary> /// <param name="path">将要被压缩的文件夹(绝对路径)</param> /// <param name="rarPath">压缩后的 .rar 的存放目录(绝对路径)</param> /// <param name="rarName">压缩文件的名称(包括后缀)</param> /// <returns></returns> public static bool RAR(string path, string rarPath, string rarName) { bool flag = false; string rarexe; //WinRAR.exe 的完整路径 RegistryKey regkey; //注册表键 Object regvalue; //键值 string cmd; //WinRAR 命令参数 ProcessStartInfo startinfo; Process process; try { regkey = Registry.ClassesRoot.OpenSubKey(WinRAR_KEY); regvalue = regkey.GetValue(""); // 键值为 "d:\Program Files\WinRAR\WinRAR.exe" "%1" rarexe = regvalue.ToString(); regkey.Close(); rarexe = rarexe.Substring(1, rarexe.Length - 7); // d:\Program Files\WinRAR\WinRAR.exe // Directory.CreateDirectory(path); //压缩命令,相当于在要压缩的文件夹(path)上点右键->WinRAR->添加到压缩文件->输入压缩文件名(rarName) cmd = string.Format("a {0} {1} -r", rarName, path); // 去掉上级目录 //cmd = string.Format("a {0} {1} -ep1", startinfo = new ProcessStartInfo(); startinfo.FileName = rarexe; startinfo.Arguments = cmd; //设置命令参数 startinfo.WindowStyle = ProcessWindowStyle.Hidden; //隐藏 WinRAR 窗口 startinfo.WorkingDirectory = rarPath; process = new Process(); process.StartInfo = startinfo; process.Start(); process.WaitForExit(); //无限期等待进程 winrar.exe 退出 if (process.HasExited) { flag = true; } process.Close(); } catch (Exception e) { throw e; } return flag; } /// 将一个文件从指定压缩文件中解压到指定的目录中 /// <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 static 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 static 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; } } } }
自己写的:
//压缩后的压缩包名称 Guid guid = Guid.NewGuid(); string yasuoName = guid + ".rar"; //压缩后的压缩包路径 string yasuoPathSave = HttpContext.Current.Server.MapPath(@"~\ExportFiles\" + DateTime.Now.ToString("yyyyMMdd") + "-zhongbiaotongzhi" + @"\" + yasuoName); if (!System.IO.File.Exists(yasuoPathSave)) { try { #region WinRAR压缩 System.Diagnostics.Process pro = new System.Diagnostics.Process(); pro.StartInfo.FileName = UtilityCommon.GetWinRARPath();//WinRAR所在路径 //启动进程的路径 pro.StartInfo.WorkingDirectory = dir; pro.StartInfo.Arguments = string.Format("a {0} {1} -ep1", yasuoName, dir);//WinRAR命令 pro.Start(); TimeSpan times = new TimeSpan(); times = pro.TotalProcessorTime; bool bo = pro.WaitForExit(60000);//设定一分钟 if (!bo) pro.Kill(); pro.Close(); pro.Dispose(); DownLoadFileHandle(yasuoPathSave); #endregion } catch (Exception) { throw; } }