.Net中使用微软WinRAR.ZIP进行压缩

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";/// /// /// /// 将要被压缩的文件夹(绝对路径)/// 压缩后的 .rar 的存放目录(绝对路径)/// 压缩文件的名称(包括后缀)/// 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);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;}/// 将一个文件从指定压缩文件中解压到指定的目录中/// /// 将一个文件从指定压缩文件中解压到指定的目录中/// /// 压缩文件/// 解压文件的目录/// 要解压文件的名称/// return成功 否则 失败public bool WinUnZipFile(string SourcePath, string path, string fileName){try{string arguments = "x " + SourcePath + " " + fileName + " " + path;return WinrarProcess(arguments);}catch{return false;}}/// 将一个文件放到压缩文件中/// /// 将一个文件放到压缩文件中/// /// 压缩文件的路径及名字/// 压缩 文件的路径/// 压缩文件的名字/// return成功 否则 失败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;}}/// /// 讲一个文件放到压缩文件中,并删除该文件/// /// 压缩文件的路径及名字/// 压缩 文件的路径/// 压缩文件的名字/// return成功 否则 失败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文件解压到指定的位置,,不带压缩目录/// /// 将一个rar文件解压到指定的位置,,不带压缩目录/// /// rar压缩文件的路径/// 解压文件要放的路径/// return成功 否则 失败public bool WinUnZip(string rarPath, string UnZippath){try{string arguments = string.Format("e \"{0}\" \"{1}\" ", rarPath, UnZippath);return WinrarProcess(arguments);}catch{return false;}}/// 将一个rar文件解压到指定的位置,不带压缩目录/// /// 将一个rar文件解压到指定的位置/// /// rar压缩文件的路径/// 解压文件要放的路径/// return成功 否则 失败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;}}/// 带目录进行压缩/// /// 带目录进行压缩/// /// 压缩文件的路径/// 被压缩文件夹的路径/// return成功 否则 失败public bool WinZipWithFile(string raPath, string filePath){try{string arguments = string.Format("a \"{0}\" \"{1}\" ", raPath, filePath);return WinrarProcess(arguments);}catch{return false;}}/// 不带目录进行压缩/// /// 不带目录进行压缩/// /// 压缩文件的路径/// 被压缩文件夹的路径/// return成功 否则 失败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 的执行程序/// /// winrar 的执行程序/// /// 要执行的winrar命令///  return成功 否则 失败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;}}}}


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部