前提:必须安装 WinRAR

1. 工具类

  1. using System;
  2. using System.Diagnostics;
  3. using System.IO;
  4. using Microsoft.Win32;
  5. namespace Util
  6. {
  7. public class RARClass
  8. {
  9. /// <summary>
  10. /// 获取WinRAR.exe路径
  11. /// </summary>
  12. /// <returns>为空则表示未安装WinRAR</returns>
  13. public static string ExistsRAR()
  14. {
  15. RegistryKey regkey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\WinRAR.exe");
  16. //RegistryKey regkey = Registry.ClassesRoot.OpenSubKey(@"Applications\WinRAR.exe\shell\open\command");
  17. string strkey = regkey.GetValue("").ToString();
  18. regkey.Close();
  19. //return strkey.Substring(1, strkey.Length - 7);
  20. return strkey;
  21. }
  22. /// <summary>
  23. /// 解压RAR文件
  24. /// </summary>
  25. /// <param name="rarFilePath">要解压的文件路径</param>
  26. /// <param name="unrarDestPath">解压路径(绝对路径)</param>
  27. public static void UnRAR(string rarFilePath, string unrarDestPath)
  28. {
  29. string rarexe = ExistsRAR();
  30. if (String.IsNullOrEmpty(rarexe))
  31. {
  32. throw new Exception("未安装WinRAR程序。");
  33. }
  34. try
  35. {
  36. //组合出需要shell的完整格式
  37. string shellArguments = string.Format("x -o+ \"{0}\" \"{1}\\\"", rarFilePath, unrarDestPath);
  38. //用Process调用
  39. using (Process unrar = new Process())
  40. {
  41. ProcessStartInfo startinfo = new ProcessStartInfo();
  42. startinfo.FileName = rarexe;
  43. startinfo.Arguments = shellArguments;               //设置命令参数
  44. startinfo.WindowStyle = ProcessWindowStyle.Hidden;  //隐藏 WinRAR 窗口
  45. unrar.StartInfo = startinfo;
  46. unrar.Start();
  47. unrar.WaitForExit();//等待解压完成
  48. unrar.Close();
  49. }
  50. }
  51. catch
  52. {
  53. throw;
  54. }
  55. }
  56. /// <summary>
  57. ///  压缩为RAR文件
  58. /// </summary>
  59. /// <param name="filePath">要压缩的文件路径(绝对路径)</param>
  60. /// <param name="rarfilePath">压缩到的路径(绝对路径)</param>
  61. public static void RAR(string filePath, string rarfilePath,  string otherPara )
  62. {
  63. RAR(filePath, rarfilePath, "", "", otherPara);
  64. }
  65. /// <summary>
  66. ///  压缩为RAR文件
  67. /// </summary>
  68. /// <param name="filePath">要压缩的文件路径(绝对路径)</param>
  69. /// <param name="rarfilePath">压缩到的路径(绝对路径)</param>
  70. /// <param name="rarName">压缩后压缩包名称</param>
  71. public static void RAR(string filePath, string rarfilePath, string rarName, string otherPara)
  72. {
  73. RAR(filePath, rarfilePath, rarName, "", otherPara);
  74. }
  75. /// <summary>
  76. ///  压缩为RAR文件
  77. /// </summary>
  78. /// <param name="filePath">要压缩的文件路径(绝对路径)</param>
  79. /// <param name="rarfilePath">压缩到的路径(绝对路径)</param>
  80. /// <param name="rarName">压缩后压缩包名称</param>
  81. /// <param name="password">解压密钥</param>
  82. public static void RAR(string filePath, string rarfilePath, string rarName, string password, string otherPara)
  83. {
  84. string rarexe = ExistsRAR();
  85. if (String.IsNullOrEmpty(rarexe))
  86. {
  87. throw new Exception("未安装WinRAR程序。");
  88. }
  89. if (!Directory.Exists(filePath))
  90. {
  91. //throw new Exception("文件不存在!");
  92. }
  93. if (String.IsNullOrEmpty(rarName))
  94. {
  95. rarName = Path.GetFileNameWithoutExtension(filePath) + ".rar";
  96. }
  97. else
  98. {
  99. if (Path.GetExtension(rarName).ToLower() != ".rar")
  100. {
  101. rarName += ".rar";
  102. }
  103. }
  104. try
  105. {
  106. //Directory.CreateDirectory(rarfilePath);
  107. //压缩命令,相当于在要压缩的文件夹(path)上点右键->WinRAR->添加到压缩文件->输入压缩文件名(rarName)
  108. string shellArguments;
  109. if (String.IsNullOrEmpty(password))
  110. {
  111. shellArguments = string.Format("a -ep1 \"{0}\" \"{1}\" -r", rarName, filePath);
  112. }
  113. else
  114. {
  115. shellArguments = string.Format("a -ep1 \"{0}\" \"{1}\" -r -p\"{2}\"", rarName, filePath, password);
  116. }
  117. if (!string.IsNullOrEmpty(otherPara))
  118. {
  119. shellArguments = shellArguments + " " + otherPara;
  120. }
  121. using (Process rar = new Process())
  122. {
  123. ProcessStartInfo startinfo = new ProcessStartInfo();
  124. startinfo.FileName = rarexe;
  125. startinfo.Arguments = shellArguments;               //设置命令参数
  126. startinfo.WindowStyle = ProcessWindowStyle.Hidden;  //隐藏 WinRAR 窗口
  127. startinfo.WorkingDirectory = rarfilePath;
  128. rar.StartInfo = startinfo;
  129. rar.Start();
  130. rar.WaitForExit(); //无限期等待进程 winrar.exe 退出
  131. rar.Close();
  132. }
  133. }
  134. catch
  135. {
  136. throw;
  137. }
  138. }
  139. }
  140. }

2. 测试程序

    1. using System;
    2. using Util;
    3. namespace ConsoleApplication2
    4. {
    5. class Program
    6. {
    7. static void Main(string[] args)
    8. {
    9. string path = "d:\\data.txt";
    10. string rarPath = "d:\\";
    11. string rarName = "";
    12. RARClass.RAR(path, rarPath, rarName, "-agYYYYMMDD -ibck");
    13. Console.WriteLine("End");
    14. Console.Read();
    15. }
    16. }
    17. }

如何用C#+WinRAR 实现压缩 分类:的更多相关文章

  1. C#利用WinRAR实现压缩和解压缩

    using System; using Microsoft.Win32; using System.Diagnostics; using System.IO; namespace MSCL { /// ...

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

    本文的原理是借助Windows平台安装的WinRAR(WinZip)实现C#程序的调用(注:WinRAR压缩解压WinZip同样适用). 先来看WinRAR(WinZip)自身的支持调用命令: 压缩命 ...

  3. 【转载】使用Winrar对压缩文件进行加密,并且给定解压密码

    有时候我们从网上下载的压缩包文件,如.rar文件.zip文件等,解压的时候需要输入解压密码才可顺利解压,否则解压失败.其实像这种情况,是压缩包制作者在压缩文件的时候对压缩文件进行了加密,输入了压缩包解 ...

  4. C# 使用WinRar命令压缩和解压缩

    using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...

  5. c#调用WinRAR软件压缩和解压文件

    using System; using System.Collections.Generic; using System.Web; using System.IO; using System.Linq ...

  6. 批处理用WINRAR只压缩某类型的文件

    1:新建文件夹sql back 和 back,在sql back 文件夹内新建test1.dbb和test2.bak文件 2:新建批处理文件rar.bat,编辑文件: @echo off for %% ...

  7. 利用YaHoo YUI实现Javascript CSS 压缩 分类: C# 2014-07-13 19:07 371人阅读 评论(0) 收藏

    网站优化时,往往需要对js文件,css文件进行压缩,以达到减少网络传输数据,减少网页加载时间:利用YaHoo的YUI可以实现Javascript,CSS,压缩,包括在线的js压缩和程序压缩,发现C#也 ...

  8. winrar 压缩文件方法

    问题描述: 我要一些大的文件进行压缩,看了网上有很多类拟的写法.但在我这都存在两个问题. 1.就是他们都是通过注册表找到rar的exe目录.我安装好winrar后,虽然注册表有,但那个目录一直报一个错 ...

  9. WinRar 压缩接压缩文件

    windows  WinRAR 定时压缩文件 命名当天时间 设置时间格式: set "Ymd=%date:~,4%%date:~5,2%%date:~8,2%" 指定 WinRAR ...

随机推荐

  1. MySQL架构优化实战系列1:数据类型与索引调优全解析

    一.数据类型优化 数据类型 整数   数字类型:整数和实数 tinyint(8).smallint(16).mediuint(24).int(32).bigint(64) 数字表示对应最大存储位数,如 ...

  2. js实现由分隔栏决定两侧div的大小—js动态分割div

    /*================index.html===================*/ <!DOCTYPE html><html lang="zh-cn&quo ...

  3. 使用CocoaPods开发并打包静态库

    Cocoapods作为OS X和iOS开发平台的类库管理工具,已经非常完善和强大.通常我们用pod来管理第三方开源类库,但我们也极有可能会开发一个用pod管理依赖关系的静态类库给其他人使用,而又不愿意 ...

  4. hibernate在使用getCurrentSession时提示no session found for current thread

    大致错误片段 org.hibernate.HibernateException: No Session found for current thread at org.springframework. ...

  5. MVC复杂模型绑定

    当初遇到业务需求ajax提交一组对象数组到服务器.但是苦于mvc的默认绑定器.绑定不上去.好吧只有靠自己了. 当初就是参考这个大大的博客:http://www.cnblogs.com/xfrog/ar ...

  6. Agent理解

    简单来说,Agent是一个交互性的技术. 比如我们在网上购物时,主体是人,即请求的发出者,我们在搜索商品时,向系统发出请求,系统会给我们一个返回结果,然而我们发现很多情况下,我们会在商品栏目中发现我们 ...

  7. JS控制flash的方法

    JS控制flash的一些方法:Play() ---------------------------------------- 播放动画 StopPlay()---------------------- ...

  8. python2-gst0.10制作静态包的补丁

    gst制作成了静态库,而python2的gst有多个动态库引用gst的库 因此,想了一个办法将python2所需要的gst打包成一个单独的共享库 办法就是,将python2_gst所有的.so先制作成 ...

  9. Emgu.CV/opencv 绘图 线面文字包括中文

    绘图很简单 Emgu.CV.Image<Bgr, Byte> image;   使用image.Draw可以画各种图形和文字包括英文及数字,不支持中文   CircleF circle = ...

  10. webpack入坑之旅

    转自: http://guowenfh.github.io/2016/03/24/vue-webpack-01-base/ http://guowenfh.github.io/2016/03/25/v ...