• 如何用C#+WinRAR 实现压缩 分类:


    前提:必须安装 WinRAR

    1. 工具类

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


    2. 测试程序

    [csharp] view plaincopy在CODE上查看代码片派生到我的代码片
     
      1. using System;  
      2. using Util;  
      3.   
      4. namespace ConsoleApplication2  
      5. {  
      6.     class Program  
      7.     {  
      8.         static void Main(string[] args)  
      9.         {  
      10.             string path = "d:\\data.txt";  
      11.             string rarPath = "d:\\";  
      12.             string rarName = "";  
      13.   
      14.             RARClass.RAR(path, rarPath, rarName, "-agYYYYMMDD -ibck");  
      15.             Console.WriteLine("End");  
      16.             Console.Read();  
      17.         }  
      18.     }  
      19. }  
  • 相关阅读:
    vscode识别预览markdown文件
    工具:静态资源服务器server
    vscode配置easy sass 生成压缩和未压缩的css文件
    cocos Create简单记录
    小程序AR云识别
    小程序被冻结 解冻方法
    vscode设置px转换为rem
    js 打印文本
    .net 启动进程并执行某方法
    c# 创建文件夹、压缩成zip格式并下载
  • 原文地址:https://www.cnblogs.com/just09161018/p/4605294.html
Copyright © 2020-2023  润新知