如何用C#+WinRAR 实现压缩 分类:
前提:必须安装 WinRAR
1. 工具类
- using System;
- using System.Diagnostics;
- using System.IO;
- using Microsoft.Win32;
- namespace Util
- {
- public class RARClass
- {
- /// <summary>
- /// 获取WinRAR.exe路径
- /// </summary>
- /// <returns>为空则表示未安装WinRAR</returns>
- public static string ExistsRAR()
- {
- RegistryKey regkey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\WinRAR.exe");
- //RegistryKey regkey = Registry.ClassesRoot.OpenSubKey(@"Applications\WinRAR.exe\shell\open\command");
- string strkey = regkey.GetValue("").ToString();
- regkey.Close();
- //return strkey.Substring(1, strkey.Length - 7);
- return strkey;
- }
- /// <summary>
- /// 解压RAR文件
- /// </summary>
- /// <param name="rarFilePath">要解压的文件路径</param>
- /// <param name="unrarDestPath">解压路径(绝对路径)</param>
- public static void UnRAR(string rarFilePath, string unrarDestPath)
- {
- string rarexe = ExistsRAR();
- if (String.IsNullOrEmpty(rarexe))
- {
- throw new Exception("未安装WinRAR程序。");
- }
- try
- {
- //组合出需要shell的完整格式
- string shellArguments = string.Format("x -o+ \"{0}\" \"{1}\\\"", rarFilePath, unrarDestPath);
- //用Process调用
- using (Process unrar = new Process())
- {
- ProcessStartInfo startinfo = new ProcessStartInfo();
- startinfo.FileName = rarexe;
- startinfo.Arguments = shellArguments; //设置命令参数
- startinfo.WindowStyle = ProcessWindowStyle.Hidden; //隐藏 WinRAR 窗口
- unrar.StartInfo = startinfo;
- unrar.Start();
- unrar.WaitForExit();//等待解压完成
- unrar.Close();
- }
- }
- catch
- {
- throw;
- }
- }
- /// <summary>
- /// 压缩为RAR文件
- /// </summary>
- /// <param name="filePath">要压缩的文件路径(绝对路径)</param>
- /// <param name="rarfilePath">压缩到的路径(绝对路径)</param>
- public static void RAR(string filePath, string rarfilePath, string otherPara )
- {
- RAR(filePath, rarfilePath, "", "", otherPara);
- }
- /// <summary>
- /// 压缩为RAR文件
- /// </summary>
- /// <param name="filePath">要压缩的文件路径(绝对路径)</param>
- /// <param name="rarfilePath">压缩到的路径(绝对路径)</param>
- /// <param name="rarName">压缩后压缩包名称</param>
- public static void RAR(string filePath, string rarfilePath, string rarName, string otherPara)
- {
- RAR(filePath, rarfilePath, rarName, "", otherPara);
- }
- /// <summary>
- /// 压缩为RAR文件
- /// </summary>
- /// <param name="filePath">要压缩的文件路径(绝对路径)</param>
- /// <param name="rarfilePath">压缩到的路径(绝对路径)</param>
- /// <param name="rarName">压缩后压缩包名称</param>
- /// <param name="password">解压密钥</param>
- public static void RAR(string filePath, string rarfilePath, string rarName, string password, string otherPara)
- {
- string rarexe = ExistsRAR();
- if (String.IsNullOrEmpty(rarexe))
- {
- throw new Exception("未安装WinRAR程序。");
- }
- if (!Directory.Exists(filePath))
- {
- //throw new Exception("文件不存在!");
- }
- if (String.IsNullOrEmpty(rarName))
- {
- rarName = Path.GetFileNameWithoutExtension(filePath) + ".rar";
- }
- else
- {
- if (Path.GetExtension(rarName).ToLower() != ".rar")
- {
- rarName += ".rar";
- }
- }
- try
- {
- //Directory.CreateDirectory(rarfilePath);
- //压缩命令,相当于在要压缩的文件夹(path)上点右键->WinRAR->添加到压缩文件->输入压缩文件名(rarName)
- string shellArguments;
- if (String.IsNullOrEmpty(password))
- {
- shellArguments = string.Format("a -ep1 \"{0}\" \"{1}\" -r", rarName, filePath);
- }
- else
- {
- shellArguments = string.Format("a -ep1 \"{0}\" \"{1}\" -r -p\"{2}\"", rarName, filePath, password);
- }
- if (!string.IsNullOrEmpty(otherPara))
- {
- shellArguments = shellArguments + " " + otherPara;
- }
- using (Process rar = new Process())
- {
- ProcessStartInfo startinfo = new ProcessStartInfo();
- startinfo.FileName = rarexe;
- startinfo.Arguments = shellArguments; //设置命令参数
- startinfo.WindowStyle = ProcessWindowStyle.Hidden; //隐藏 WinRAR 窗口
- startinfo.WorkingDirectory = rarfilePath;
- rar.StartInfo = startinfo;
- rar.Start();
- rar.WaitForExit(); //无限期等待进程 winrar.exe 退出
- rar.Close();
- }
- }
- catch
- {
- throw;
- }
- }
- }
- }
2. 测试程序
- using System;
- using Util;
- namespace ConsoleApplication2
- {
- class Program
- {
- static void Main(string[] args)
- {
- string path = "d:\\data.txt";
- string rarPath = "d:\\";
- string rarName = "";
- RARClass.RAR(path, rarPath, rarName, "-agYYYYMMDD -ibck");
- Console.WriteLine("End");
- Console.Read();
- }
- }
- }
如何用C#+WinRAR 实现压缩 分类:的更多相关文章
- C#利用WinRAR实现压缩和解压缩
using System; using Microsoft.Win32; using System.Diagnostics; using System.IO; namespace MSCL { /// ...
- WinRAR(WinZip)压缩与解压实现(C#版Window平台)
本文的原理是借助Windows平台安装的WinRAR(WinZip)实现C#程序的调用(注:WinRAR压缩解压WinZip同样适用). 先来看WinRAR(WinZip)自身的支持调用命令: 压缩命 ...
- 【转载】使用Winrar对压缩文件进行加密,并且给定解压密码
有时候我们从网上下载的压缩包文件,如.rar文件.zip文件等,解压的时候需要输入解压密码才可顺利解压,否则解压失败.其实像这种情况,是压缩包制作者在压缩文件的时候对压缩文件进行了加密,输入了压缩包解 ...
- C# 使用WinRar命令压缩和解压缩
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...
- c#调用WinRAR软件压缩和解压文件
using System; using System.Collections.Generic; using System.Web; using System.IO; using System.Linq ...
- 批处理用WINRAR只压缩某类型的文件
1:新建文件夹sql back 和 back,在sql back 文件夹内新建test1.dbb和test2.bak文件 2:新建批处理文件rar.bat,编辑文件: @echo off for %% ...
- 利用YaHoo YUI实现Javascript CSS 压缩 分类: C# 2014-07-13 19:07 371人阅读 评论(0) 收藏
网站优化时,往往需要对js文件,css文件进行压缩,以达到减少网络传输数据,减少网页加载时间:利用YaHoo的YUI可以实现Javascript,CSS,压缩,包括在线的js压缩和程序压缩,发现C#也 ...
- winrar 压缩文件方法
问题描述: 我要一些大的文件进行压缩,看了网上有很多类拟的写法.但在我这都存在两个问题. 1.就是他们都是通过注册表找到rar的exe目录.我安装好winrar后,虽然注册表有,但那个目录一直报一个错 ...
- WinRar 压缩接压缩文件
windows WinRAR 定时压缩文件 命名当天时间 设置时间格式: set "Ymd=%date:~,4%%date:~5,2%%date:~8,2%" 指定 WinRAR ...
随机推荐
- BZOJ 3781: 小B的询问
3781: 小B的询问 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 643 Solved: 435[Submit][Status][Discuss ...
- BZOJ 1861: [Zjoi2006]Book 书架
1861: [Zjoi2006]Book 书架 Time Limit: 4 Sec Memory Limit: 64 MBSubmit: 1290 Solved: 740[Submit][Stat ...
- oracle中将自建用户下的所有表删除
select 'drop table '||table_name||' ;' from user_tables;select 'drop sequence '||sequence_name||' ;' ...
- 优化php代码 - 字符串echo输出 逗号也可作php连接符
2016年12月12日10:00:16 ====================== 网页访问速度的提升,是可以通过代码的优化来实现的.代码的优化,并不是说代码越少越好,而是主要看代码的运行能力和执行 ...
- Protocol Framework - SNMP Tutorial
30.4 Protocol Framework TCP/IP network management protocols2 divide the management problem into two ...
- Socket异步通讯
1.可以通过多线程来解决(一会补上) 2.Socket在tcp/udp两种通信协议下的异步通信: 基于TCP的异步通信: BeginAccept方法和endeaccept方法 包含在System.Ne ...
- 主流ORM对比分析,莫人云亦云
目前主流的ORM框架有Entity Framework,Dapper,NHibernate,NBear,Castle ActiveRecord,BATIS.NET六种,都是免费开源的.下边从官方支持性 ...
- [UML]UML系列——类图class的实现关系Realization
系列文章 [UML]UML系列——用例图Use Case [UML]UML系列——用例图中的各种关系(include.extend) [UML]UML系列——类图Class ...
- tyvj1011 传纸条
背景 NOIP2008复赛提高组第三题 描述 小渊和小轩是好朋友也是同班同学,他们在一起总有谈不完的话题.一次素质拓展活动中,班上同学安排做成一个m行n列的矩阵,而小渊和小轩被安排在矩阵对角线的两端, ...
- [IOS基础]关于IOS的UIScreeen,UIView,UIViewController,UIWindow理解
UIScreen: 代表当前这个屏幕,通过UIApplication可以获得这个属性 UIView: 一个矩形试图,包含用户手势和时间响应 UIViewController: 一个UIView的集 ...