WinRAR(WinZip)压缩与解压实现(C#版Window平台)

本文的原理是借助Windows平台安装的WinRAR(WinZip)实现C#程序的调用(注:WinRAR压缩解压WinZip同样适用)。

 

先来看WinRAR(WinZip)自身的支持调用命令:

压缩命令:a {0} {1} -r 【{0}:压缩后文件名|{1}:待压缩的文件物理路径】

ex:"a 你妹.rar f:\\MM -r" (含义为将f盘下MM的文件夹压缩为"你妹.rar"文件)

解压命令:x {0} {1} -y 【{0}:待解压文件名称|{1}:待解压文件物理路径】

ex:"x 幺妹.rar f:\\幺妹 -y"(待压缩文件物理路径:"f:\\幺妹\\幺妹.rar")

 

参数说明

参数

含义

a

添加文件到压缩包

x

以完整路径从压缩包解开压缩

 

 

 

 

 

WinZip(WinRAR)调用通用类

using System;
using System.Collections.Generic;
using System.Text;

//--------------using
using System.Diagnostics;
using Microsoft.Win32;
using System.IO;


/// <summary>
/// Name:Stone
/// DateTime: 2011/12/31 16:39:26
/// Description:WinRAR压缩
/// </summary>
public class WinRARCSharp
{
// WinRAR安装注册表key
private const string WinRAR_KEY = @"WinRAR.ZIP\shell\open\command";

/// <summary>
/// 利用 WinRAR 进行压缩
/// </summary>
/// <param name="path">将要被压缩的文件夹(绝对路径)</param>
/// <param name="rarPath">压缩后的 .rar 的存放目录(绝对路径)</param>
/// <param name="rarName">压缩文件的名称(包括后缀)</param>
/// <returns>true 或 false。压缩成功返回 true,反之,false。</returns>
public 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;
}
/// <summary>
/// 利用 WinRAR 进行解压缩
/// </summary>
/// <param name="path">文件解压路径(绝对)</param>
/// <param name="rarPath">将要解压缩的 .rar 文件的存放目录(绝对路径)</param>
/// <param name="rarName">将要解压缩的 .rar 文件名(包括后缀)</param>
/// <returns>true 或 false。解压缩成功返回 true,反之,false。</returns>
public bool UnRAR(string path, string rarPath, string rarName)
{
bool flag = false;
string rarexe;
RegistryKey regkey;
Object regvalue;
string cmd;
ProcessStartInfo startinfo;
Process process;
try
{
regkey = Registry.ClassesRoot.OpenSubKey(WinRAR_KEY);
regvalue = regkey.GetValue("");
rarexe = regvalue.ToString();
regkey.Close();
rarexe = rarexe.Substring(1, rarexe.Length - 7);

Directory.CreateDirectory(path);
//解压缩命令,相当于在要压缩文件(rarName)上点右键->WinRAR->解压到当前文件夹
cmd = string.Format("x {0} {1} -y",
rarName,
path);
startinfo = new ProcessStartInfo();
startinfo.FileName = rarexe;
startinfo.Arguments = cmd;
startinfo.WindowStyle = ProcessWindowStyle.Hidden;

startinfo.WorkingDirectory = rarPath;
process = new Process();
process.StartInfo = startinfo;
process.Start();
process.WaitForExit();
if (process.HasExited)
{
flag = true;
}
process.Close();
}
catch (Exception e)
{
throw e;
}
return flag;
}
}

调用方法

WinRARCSharp win = new WinRARCSharp();

win.RAR("F:\\aaa\\", "f:\\", "a.rar"); // 压缩(将“f:\\aaa\\”目录文件压缩到“f:\\a.rar”)

win.UnRAR("f:\\呦M.zip", "f:\\MM", "GG"); // 解压(将“f:\\呦M.zip”解压到“f:\\MM\\GG”目录下)

 

7z压缩通用类:

using System;
using System.Collections.Generic;
using System.Text;

using Microsoft.Win32;
using System.Diagnostics;
using System.IO;


/// <summary>
/// Name:Stone
/// DateTime: 2012/1/4 16:26:08
/// Description:7Z解压管理类
/// </summary>
public class _7zRAR
{

// 7z.exe 安装地址
private const string _7zEXE = @"D:\Program Files (x86)\7-Zip\7z.exe";

/// <summary>
/// 利用 7zExE 进行压缩
/// </summary>
/// <param name="_7zPath">将要被压缩的文件夹(物理路径)</param>
/// <param name="filePath">压缩后的的存放目录(物理路径)</param>
/// <returns>true 或 false。压缩成功返回 true,反之,false。</returns>
public static bool Un7zRAR(string _7zPath, string filePath)
{
bool flag = false;

string cmd;
ProcessStartInfo startinfo;
Process process;
try
{
cmd = String.Format(@"x {0} -o{1} -y",
_7zPath, filePath);
startinfo = new ProcessStartInfo();
startinfo.FileName = _7zEXE;
startinfo.Arguments = cmd;
startinfo.WindowStyle = ProcessWindowStyle.Hidden;

process = new Process();
process.StartInfo = startinfo;
process.Start();
process.WaitForExit();
if (process.HasExited)
{
flag = true;
}
process.Close();
}
catch (Exception e)
{
throw e;
}
return flag;
}
}


 

 




=============《完》===================
转载请写明出处:http://www.cnblogs.com/stone_w/archive/2012/01/04/2312294.html

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/547631.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Docker Consul 安装及使用服务发现

更多配置和原理&#xff1a;https://blog.csdn.net/liuzhuchen/article/details/81913562 从2016年起就开始接触Consul&#xff0c;使用的主要目的就是做服务发现&#xff0c;后来逐步应用于生产环境&#xff0c;并总结了少许使用经验。最开始使用Consul的人不多&#xff0c;为…

Visual Studio扩展工具添加与卸载

简介&#xff1a;vs 作为主流的开发工具之一&#xff0c;其强大的功能无可厚非&#xff0c;但日益增加的需求就使得vs的扩展工具成为优秀dev所必备的利器之一。 Visual Studio扩展工具添加 Visual Studio扩展工具的添加很简单&#xff0c;只需要选择需要的扩展工具然后一般安装…

python代码风格检查工具──pylint

pylint是一个python代码检查工具&#xff0c;可以帮助python程序员方便地检查程序代码的语法和风格&#xff0c;通过这个工具&#xff0c;可以使你的python代码尽量保持完美&#xff0c;哈哈。具体可以检查什么东西呢&#xff1f;比如你写了 from XXX import * 了&#xff0c;它…

php充值注入,PHP注入一路小跑

PHP注入一路小跑[ 2006-04-20 14:16:55 | 作者: 承諾 ]字体大小: 大 | 中 | 小很老的了&#xff0c;我是给我自己看的。忘了好多&#xff0c;补习一下‘ or ‘11‘/*‘%23‘ and password‘mypassid-1 union select 1,1,1id-1 union select char(97),char(97),char(97)id1 unio…

Visual Studio 扩展包(.vsix)制作

前言&#xff1a;上篇介绍了 Visual Studio扩展工具添加与卸载&#xff0c;本编要介绍的是Visual Studio 扩展包(.vsix)的制作。 方法&#xff1a; ①、下载并安装Visual Studio 2010 SDK。 vs 2010 开发工具下载SDK安装包官方下载地址&#xff1a;http://www.microsoft.com/d…

php5..6中文帮助,6.5. IDE integration

IDE 集成理想情况下&#xff0c;应用程序代码不应直接使用DI容器&#xff1a;应首选依赖项注入。但是&#xff0c;在某些情况下&#xff0c;可能会直接调用容器&#xff1a;编写根应用程序类(前端控制器等)或更通用的框架时编写工厂时维护或迁移旧版应用程序时编写功能测试时..…

公主病 - 百度百科

http://wapbaike.baidu.com/view/287227.htm?ssid0&from844b&uid3151E6C0905477A13653132D762BB6FB&pusz%401320_1001%2Cta%40iphone_2_4.1_3_537%2Cusm%403&bd_page_type1&tjXk_1_0_10_title

CDH6.2 Linux离线安装

1.概述 CDH&#xff0c;全称Clouderas Distribution, including Apache Hadoop。是Hadoop众多分支中对应中的一种&#xff0c;由Cloudera维护&#xff0c;基于稳定版本的Apache Hadoop构建&#xff0c;提供了Hadoop的核心&#xff08;可扩展存储、分布式计算&#xff09;&#…

vs怎么调试php程序,vscode如何调试运行c#程序

前提条件&#xff1a;安装.NET Core SDK安装vscode步骤&#xff1a;安装c#extension插件创建第一个项目&#xff0c;手动创建workspace文件夹在vscode中使用快捷键CTRLKCTRLO选择刚才创建的文件夹使用快捷键CTRLSHIFITY打开控制台使用下面的命令在打开的终端里面创建一个基础的…

Openshift 4.4 静态 IP 离线安装系列:准备离线资源

本系列文章描述了离线环境下以 UPI (User Provisioned Infrastructure) 模式安装 Openshift Container Platform (OCP) 4.4.5 的步骤&#xff0c;我的环境是 VMware ESXI 虚拟化&#xff0c;也适用于其他方式提供的虚拟机或物理主机。离线资源包括安装镜像、所有样例 Image Str…

[转]Install Windows Server 2012 in VMware Workstation

本文转自&#xff1a;http://kb4you.wordpress.com/2012/06/28/install-windows-server-2012-in-vmware-workstation-2/ This procedure describes how to install Windows Server 2012 in VMware Workstation. The following versions are used: VMware Workstation Technolog…

生成文件的另一种思路——共享文件同步

背景 由于网站访问量大&#xff0c;需要多台服务器生成静态文件&#xff0c;然后多机负载&#xff0c;所有生成成了头等大事&#xff0c;一是方式所需&#xff0c;二是生成环节消耗CPU与内存操作太大&#xff0c;经常出问题。常用的生成方式&#xff1a;1.多台服务器&#xff…

php window.onload,tp_window.onload+相应操作

[php]代码库window.οnlοadfunction(){if(0){document.getElementsByName(sex)[1].checkedchecked;}else{document.getElementsByName(sex)[0].checkedchecked;}}class UserAction extends Action{public function index(){$mM(User);$arr$m->select();//var_dump($arr);$t…

Openshift 4.4 静态 IP 离线安装系列:初始安装

Openshift 4.4 静态 IP 离线安装系列&#xff1a;初始安装 上篇文章准备了离线安装 OCP 所需要的离线资源&#xff0c;包括安装镜像、所有样例 Image Stream 和 OperatorHub 中的所有 RedHat Operators。本文就开始正式安装 OCP&#xff08;Openshift Container Platform&…

【JavaScript学习】JavaScript对象创建

1.最简单的方法&#xff0c;创建一个对象&#xff0c;然后添加属性 1 var person new Object();2 person.age 23;3 person.name "David";4 person.job "student";5 person.sayName function ()6 {7 alert(this.name);8 };9 10 //类似于定义键…

C# ArrayList 与 string、string[] 的转换

1、ArrarList 转换为 string[] ArrayList list new ArrayList(); list.Add("aaa"); list.Add("bbb"); //转换成数组 string[] arrString (string[])list.ToArray(typeof( string)); 2、string[] 转换为 ArrarList ArrayList list new ArrayList(new st…

oracle不能访问管理页面,Oracle Grid Control CONSOLE无法打开9i数据库的管理维护页面...

今天在Solaris平台的测试环境上安装了Oracle Grid control 10.2.0.1&#xff0c;安装及配置完成后&#xff0c;发现在登录9i数据库的tablespace维护页面时&#xff0c;页面处于长时间的等待状况。最终返回错误信息。该页面在打开其他较小的数据库的页面时&#xff0c;均能正常访…

(转)在Myeclipse中查看android源码就是这么easy

http://byandby.iteye.com/blog/814277转载于:https://www.cnblogs.com/hyzhou/p/3217022.html

SQL Server 不同数据库导入指定数据解决方案

1 use 待导入DB 2 go 3 4 /*启动Ad Hoc Distributed Queries*/ 5 exec sp_configure show advanced options,1 6 reconfigure 7 exec sp_configure Ad Hoc Distributed Queries,1 8 reconfigure 9 10 insert into 待导入DB.dbo.表名11 select top 10 sid from opendatasource(…

Vue + Element UI 实现 登陆注册基本demo实例

Vue Element UI 实现权限管理系统 前端篇&#xff08;二&#xff09;&#xff1a;Vue Element 案例 导入项目 打开 Visual Studio Code&#xff0c;File --> add Folder to Workspace&#xff0c;导入我们的项目。 安装 Element 安装依赖 Element 是国内饿了么公司提供…