如何用C#+WinRAR 实现压缩

前提:必须安装 WinRAR

1. 工具类

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

namespace Util
{
    public class RARClass
    {
        /// 
        /// 获取WinRAR.exe路径
        /// 
        /// 为空则表示未安装WinRAR
        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;
        }

        /// 
        /// 解压RAR文件
        /// 
        /// 要解压的文件路径
        /// 解压路径(绝对路径)
        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;
            }
        }

        /// 
        ///  压缩为RAR文件
        /// 
        /// 要压缩的文件路径(绝对路径)
        /// 压缩到的路径(绝对路径)
        public static void RAR(string filePath, string rarfilePath,  string otherPara )
        {
            RAR(filePath, rarfilePath, "", "", otherPara);
        }

        /// 
        ///  压缩为RAR文件
        /// 
        /// 要压缩的文件路径(绝对路径)
        /// 压缩到的路径(绝对路径)
        /// 压缩后压缩包名称
        public static void RAR(string filePath, string rarfilePath, string rarName, string otherPara)
        {
            RAR(filePath, rarfilePath, rarName, "", otherPara);
        }

        /// 
        ///  压缩为RAR文件
        /// 
        /// 要压缩的文件路径(绝对路径)
        /// 压缩到的路径(绝对路径)
        /// 压缩后压缩包名称
        /// 解压密钥
        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#)