如何用C#+WinRAR 实现压缩
来源:互联网 发布:鞍山网络网吧维护 编辑:程序博客网 时间:2024/04/28 14:50
前提:必须安装 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(); } }}
1 0
- 如何用C#+WinRAR 实现压缩
- 如何用WINRAR制作安装程序
- 如何用 WinRAR 批量解压文件?
- 如何用WinRAR制作安装包
- 如何用WINRAR制作安装程序
- 如何用c实现可执行文件的复制
- 如何用C实现C++的特性
- 如何用c语言实现多态
- 如何用C语言实现面向对象
- 如何用C语言实现三子棋游戏
- 如何用BitmapFactory来实现图片的简单压缩
- 在线实现WINRAR压缩和解压缩
- C#利用WinRAR实现压缩和解压缩
- 如何用C画图
- 如何用命令行生成WinRAR自解压文件
- 如何用WinRAR解压缩7z分卷文件
- 如何用程序实现远程桌面连接(C#)
- 如何用Java实现类似c/c++指针效果
- sleep()和yield()方法的区别
- spark:sortByKey按年龄排序--20
- ZJNU-2094-D-鬼来了
- 九度OJ-1113-二叉树
- 于生打造乾坤会第一礼品-锦裕鱼罐头
- 如何用C#+WinRAR 实现压缩
- JSP编程,url中加斜杠和不加斜杠的区别
- R语言基础编程技巧汇编 - 14
- iOSBlock实现传值
- 阿里笔试感想
- Java常量定义
- P51-8
- [笔试时]SQL练练手
- c语言005---执行的步骤