C#解壓RAR壓縮檔(–轉載–測試通過)

來源:互聯網
上載者:User

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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.