天天看點

C#利用WinRAR實作壓縮和解壓縮

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

namespace MSCL
{
    /// <summary>
    /// 壓縮解壓類
    /// </summary>
    public class ZipHelper
    {
        /// <summary>
        /// 利用 WinRAR 進行壓縮
        /// </summary>
        /// <param name="path">将要被壓縮的檔案夾(絕對路徑)</param>
        /// <param name="rarPath">壓縮後的 .rar 的存放目錄(絕對路徑)</param>
        /// <param name="rarName">壓縮檔案的名稱(包括字尾)</param>
        /// <returns>true 或 false。壓縮成功傳回 true,反之,false。</returns>
        public bool RAR(string path, string rarPath, string rarName)
        {
            bool flag = false;
            string rarexe;       //WinRAR.exe 的完整路徑
            RegistryKey regkey;  //系統資料庫鍵
            Object regvalue;     //鍵值
            string cmd;          //WinRAR 指令參數
            ProcessStartInfo startinfo;
            Process process;
            try
            {
                regkey = Registry.ClassesRoot.OpenSubKey(@"Applications\WinRAR.exe\shell\open\command");
                regvalue = regkey.GetValue("WinRAR");  // 鍵值為 "d:\Program Files\WinRAR\WinRAR.exe" "%1"
                rarexe = regvalue.ToString();
                regkey.Close();
                rarexe = rarexe.Substring(1, rarexe.Length - 7);  // d:\Program Files\WinRAR\WinRAR.exe

                Directory.CreateDirectory(path);
                //壓縮指令,相當于在要壓縮的檔案夾(path)上點右鍵->WinRAR->添加到壓縮檔案->輸入壓縮檔案名(rarName)
                cmd = string.Format("a {0} {1} -r",
                                    rarName,
                                    path);
                startinfo = new ProcessStartInfo();
                startinfo.FileName = rarexe;
                startinfo.Arguments = cmd;                          //設定指令參數
                startinfo.WindowStyle = ProcessWindowStyle.Hidden;  //隐藏 WinRAR 視窗

                startinfo.WorkingDirectory = rarPath;
                process = new Process();
                process.StartInfo = startinfo;
                process.Start();
                process.WaitForExit(); //無限期等待程序 winrar.exe 退出
                if (process.HasExited)
                {
                    flag = true;
                }
                process.Close();
            }
            catch (Exception e)
            {
                throw e;
            }
            return flag;
        }


        /// <summary>
        /// 利用 WinRAR 進行解壓縮
        /// </summary>
        /// <param name="path">檔案解壓路徑(絕對)</param>
        /// <param name="rarPath">将要解壓縮的 .rar 檔案的存放目錄(絕對路徑)</param>
        /// <param name="rarName">将要解壓縮的 .rar 檔案名(包括字尾)</param>
        /// <returns>true 或 false。解壓縮成功傳回 true,反之,false。</returns>
        public bool UnRAR(string path, string rarPath, string rarName)
        {
            bool flag = false;
            string rarexe;
            RegistryKey regkey;
            Object regvalue;
            string cmd;
            ProcessStartInfo startinfo;
            Process process;
            try
            {
                regkey = Registry.ClassesRoot.OpenSubKey(@"Applications\WinRAR.exe\shell\open\command");
                regvalue = regkey.GetValue("");
                rarexe = regvalue.ToString();
                regkey.Close();
                rarexe = rarexe.Substring(1, rarexe.Length - 7);

                Directory.CreateDirectory(path);
                //解壓縮指令,相當于在要壓縮檔案(rarName)上點右鍵->WinRAR->解壓到目前檔案夾
                cmd = string.Format("x {0} {1} -y",
                                    rarName,
                                    path);
                startinfo = new ProcessStartInfo();
                startinfo.FileName = rarexe;
                startinfo.Arguments = cmd;
                startinfo.WindowStyle = ProcessWindowStyle.Hidden;

                startinfo.WorkingDirectory = rarPath;
                process = new Process();
                process.StartInfo = startinfo;
                process.Start();
                process.WaitForExit();
                if (process.HasExited)
                {
                    flag = true;
                }
                process.Close();
            }
            catch (Exception e)
            {
                throw e;
            }
            return flag;
        }
    }
}