C# 利用WinRAR (加密)壓縮及解壓縮 相關檔案夾及檔案

來源:互聯網
上載者:User

本次樣本主要實現:
1.壓縮檔夾及其下檔案
2.壓縮檔夾下檔案
3.壓縮檔夾及其下檔案為rar 還是 zip
4.解壓縮
5.加密壓縮及解加密壓縮
-----------
範例程式碼如下:
protected void Button1_Click(object sender, EventArgs e)
    {
        string strtxtPath = "C:\\freezip\\free.txt";
        string strzipPath = "C:\\freezip\\free.zip";
        System.Diagnostics.Process Process1 = new System.Diagnostics.Process();
        Process1.StartInfo.FileName = "Winrar.exe";
        Process1.StartInfo.CreateNoWindow = true;

        //// 1
        ////壓縮c:\freezip\free.txt(即檔案夾及其下檔案freezip\free.txt)
        ////到c:\freezip\free.rar
        //strzipPath = "C:\\freezip\\free";//預設壓縮方式為 .rar
        //Process1.StartInfo.Arguments = " a -r " + strzipPath + " " + strtxtPath;

        //// 2
        ////壓縮c:\freezip\free.txt(即檔案夾及其下檔案freezip\free.txt)
        ////到c:\freezip\free.rar
        //strzipPath = "C:\\freezip\\free";//設定壓縮方式為 .zip
        //Process1.StartInfo.Arguments = " a -afzip " + strzipPath + " " + strtxtPath;

        //// 3
        ////壓縮c:\freezip\free.txt(即檔案夾及其下檔案freezip\free.txt)
        ////到c:\freezip\free.zip  直接設定為free.zip
        //Process1.StartInfo.Arguments = " a -r "+strzipPath+" " + strtxtPath ;

        //// 4
        ////搬遷壓縮c:\freezip\free.txt(即檔案夾及其下檔案freezip\free.txt)
        ////到c:\freezip\free.rar 壓縮後 原檔案將不存在
        //Process1.StartInfo.Arguments = " m " + strzipPath + " " + strtxtPath;

        //// 5
        ////壓縮c:\freezip\下的free.txt(即檔案free.txt)
        ////到c:\freezip\free.zip  直接設定為free.zip 只有檔案 而沒有檔案夾
        //Process1.StartInfo.Arguments = " a -ep " + strzipPath + " " + strtxtPath;

        //// 6
        ////解壓縮c:\freezip\free.rar
        ////到 c:\freezip\
        //strtxtPath = "c:\\freezip\\";
        //Process1.StartInfo.Arguments = " x " + strzipPath + " " + strtxtPath;

        //// 7
        ////加密壓縮c:\freezip\free.txt(即檔案夾及其下檔案freezip\free.txt)
        ////到c:\freezip\free.zip  密碼為123456 注意參數間不要空格
        //Process1.StartInfo.Arguments = " a -p123456 " + strzipPath + " " + strtxtPath;

        //// 8
        ////解壓縮加密的c:\freezip\free.rar
        ////到 c:\freezip\   密碼為123456 注意參數間不要空格
        //strtxtPath = "c:\\freezip\\";
        //Process1.StartInfo.Arguments = " x -p123456 " + strzipPath + " " + strtxtPath;

       //// 9 
       ////-o+ 覆蓋 已經存在的檔案
       //// -o- 不覆蓋 已經存在的檔案
       //strtxtPath = "c:\\freezip\\";
       //Process1.StartInfo.Arguments = " x -o+ " + strzipPath + " " + strtxtPath;

       ////10
       //// 只從指定的zip中
       //// 解壓出free1.txt
       //// 到指定路徑下
       //// 壓縮包中的其他檔案 不予解壓
       //strtxtPath = "c:\\freezip\\";
       //Process1.StartInfo.Arguments = " x " + strzipPath + " " +" free1.txt" + " " + strtxtPath;

      //// 11
      //// 通過 -y 對所有詢問回應為"是" 以便 即便發生錯誤 也不彈出WINRAR的視窗
      //// -cl 轉換檔名為小寫字母 
      //strtxtPath = "c:\\freezip\\";
      //Process1.StartInfo.Arguments = " t -y -cl " + strzipPath + " " + " free1.txt";

        Process1.Start();   
        if (Process1.HasExited)
        {
                int iExitCode = Process1.ExitCode;
                if (iExitCode == 0)
                {
                    Response.Write(iExitCode.ToString() + " 正常完成");
                }
                else
                {
                    Response.Write(iExitCode.ToString() + " 有錯完成");
                }
        }
    }

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.