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

本次示例主要實現:
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() + " 有錯完成");
                }
        }
    } 

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