C#解壓RAR壓縮文件

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using Microsoft.Win32;
using System.Diagnostics;

namespace Uni.UniCustoms
{
    public class clsWinrar
    {
        /// <summary>
        /// 是否安裝了Winrar
        /// </summary>
        /// <returns></returns>
        static public bool Exists()
        {
            RegistryKey the_Reg = Registry.LocalMachine.OpenSubKey(@"SOFTWARE/Microsoft/Windows/CurrentVersion/App Paths/WinRAR.exe");
            return !string.IsNullOrEmpty(the_Reg.GetValue("").ToString());
        }

        /// <summary>
        /// 打包成Rar
        /// </summary>
        /// <param name="patch"></param>
        /// <param name="rarPatch"></param>
        /// <param name="rarName"></param>
        public void CompressRAR(string patch, string rarPatch, string rarName)
        {
            string the_rar;
            RegistryKey the_Reg;
            object the_Obj;
            string the_Info;
            ProcessStartInfo the_StartInfo;
            Process the_Process;
            try
            {
                the_Reg = Registry.LocalMachine.OpenSubKey(@"SOFTWARE/Microsoft/Windows/CurrentVersion/App Paths/WinRAR.exe");
                the_Obj = the_Reg.GetValue("");
                the_rar = the_Obj.ToString();
                the_Reg.Close();
                the_rar = the_rar.Substring(1, the_rar.Length - 7);
                Directory.CreateDirectory(patch);
                //命令參數
                //the_Info = " a    " + rarName + " " + @"C:Test?70821.txt"; //文件壓縮
                the_Info = " a    " + rarName + " " + patch + " -r"; ;
                the_StartInfo = new ProcessStartInfo();
                the_StartInfo.FileName = the_rar;
                the_StartInfo.Arguments = the_Info;
                the_StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
                //打包文件存放目錄
                the_StartInfo.WorkingDirectory = rarPatch;
                the_Process = new Process();
                the_Process.StartInfo = the_StartInfo;
                the_Process.Start();
                the_Process.WaitForExit();
                the_Process.Close();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 解壓
        /// </summary>
        /// <param name="unRarPatch"></param>
        /// <param name="rarPatch"></param>
        /// <param name="rarName"></param>
        /// <returns></returns>
        public string unCompressRAR(string unRarPatch, string rarPatch, string rarName)
        {
            string the_rar;
            RegistryKey the_Reg;
            object the_Obj;
            string the_Info;


            try
            {
                the_Reg = Registry.LocalMachine.OpenSubKey(@"SOFTWARE/Microsoft/Windows/CurrentVersion/App Paths/WinRAR.exe");
                the_Obj = the_Reg.GetValue("");
                the_rar = the_Obj.ToString();
                the_Reg.Close();
                //the_rar = the_rar.Substring(1, the_rar.Length - 7);

                if (Directory.Exists(unRarPatch) == false)
                {
                    Directory.CreateDirectory(unRarPatch);
                }
                the_Info = "x " + rarName + " " + unRarPatch + " -y";

                ProcessStartInfo the_StartInfo = new ProcessStartInfo();
                the_StartInfo.FileName = the_rar;
                the_StartInfo.Arguments = the_Info;
                the_StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
                the_StartInfo.WorkingDirectory = rarPatch;//獲取壓縮包路徑

                Process the_Process = new Process();
                the_Process.StartInfo = the_StartInfo;
                the_Process.Start();
                the_Process.WaitForExit();
                the_Process.Close();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return unRarPatch;
        }
    }
}

RAR參數:
一、壓縮命令
1、將temp.txt壓縮爲temp.rarrar a temp.rar temp.txt
2、將當前目錄下所有文件壓縮到temp.rarrar a temp.rar *.*
3、將當前目錄下所有文件及其所有子目錄壓縮到temp.rarrar a temp.rar *.* -r
4、將當前目錄下所有文件及其所有子目錄壓縮到temp.rar,並加上密碼123rar a temp.rar *.* -r -p123
二、解壓命令
1、將temp.rar解壓到c:/temp目錄rar e temp.rar c:/temprar e *.rar c:/temp(支持批量操作)
2、將temp.rar解壓到c:/temp目錄,並且解壓後的目錄結構和temp.rar中的目錄結構一


壓縮目錄test及其子目錄的文件內容
Wzzip test.zip test -r -P
WINRAR A test.rar test -r

刪除壓縮包中的*.txt文件
Wzzip test.zip *.txt -d
WinRAR d test.rar *.txt


刷新壓縮包中的文件,即添加已經存在於壓縮包中但更新的文件
Wzzip test.zip test -f
Winrar f test.rar test


更新壓縮包中的文件,即添加已經存在於壓縮包中但更新的文件以及新文件
Wzzip test.zip test -u
Winrar u test.rar test


移動文件到壓縮包,即添加文件到壓縮包後再刪除被壓縮的文件
Wzzip test.zip -r -P -m
Winrar m test.rar test -r


添加全部 *.exe 文件到壓縮文件,但排除有 a或b
開頭名稱的文件
Wzzip test *.exe -xf*.* -xb*.*
WinRAR a test *.exe -xf*.* -xb*.*


加密碼進行壓縮
Wzzip test.zip test
-s123。注意密碼是大小寫敏感的。在圖形界面下打開帶密碼的壓縮文件,會看到+號標記(附圖1)。
WINRAR A test.rar test -p123
-r。注意密碼是大小寫敏感的。在圖形界面下打開帶密碼的壓縮文件,會看到*號標記(附圖2)。


按名字排序、以簡要方式列表顯示壓縮包文件
Wzzip test.zip -vbn
Rar l test.rar


鎖定壓縮包,即防止未來對壓縮包的任何修改
無對應命令
Winrar k test.rar


創建360kb大小的分卷壓縮包
無對應命令
Winrar a -v360 test


帶子目錄信息解壓縮文件
Wzunzip test -d
Winrar x test -r


不帶子目錄信息解壓縮文件
Wzunzip test
Winrar e test


解壓縮文件到指定目錄,如果目錄不存在,自動創建
Wzunzip test newfolder
Winrar x test newfolder


解壓縮文件並確認覆蓋文件
Wzunzip test -y
Winrar x test -y


解壓縮特定文件
Wzunzip test *.txt
Winrar x test *.txt


解壓縮現有文件的更新文件
Wzunzip test -f
Winrar x test -f


解壓縮現有文件的更新文件及新文件
Wzunzip test -n
Winrar x test -u


批量解壓縮文件
Wzunzip *.zip
WinRAR e *.rar

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章