Asp.net調用RAR壓縮 解壓檔案

來源:互聯網
上載者:User

    //壓縮
    protected void btnY_Click(object sender, EventArgs e)
    {
        string rar;
        RegistryKey reg;
        string args;
        ProcessStartInfo procStart;
        Process process;
        try
        {
            reg = Registry.ClassesRoot.OpenSubKey(@"Applications\WinRAR.exe\Shell\Open\Command");
            rar = reg.GetValue("").ToString();//擷取註冊表rar安裝路徑
            reg.Close();
            rar = rar.Substring(1, rar.Length - 7);//擷取rar安裝路徑
            args = "a -inul -y G:\\temp.rar G:\\1.txt";//這裡為rar的壓縮命令格式(也可以自行擴充)
            procStart = new ProcessStartInfo();
            procStart.FileName = rar;
            procStart.Arguments = args;//參數
            procStart.WindowStyle = ProcessWindowStyle.Hidden;//視窗狀態
            procStart.WorkingDirectory = Server.MapPath(""); ;//擷取或設定要啟動的進程的初始目錄。
            process = new Process();
            process.StartInfo = procStart;
            process.Start();
            Response.Write("<script>alert('壓縮成功')</script>");
        }
        catch (Exception ex)
        {
            Response.Write(ex.ToString());
        }
    }
    //解壓
    protected void btnJ_Click(object sender, EventArgs e)
    {
        string rar;
        RegistryKey reg;
        string args;
        ProcessStartInfo startInfo;
        Process process;
        try
        {
            reg = Registry.ClassesRoot.OpenSubKey(@"Applications\WinRar.exe\Shell\Open\Command");
            rar = reg.GetValue("").ToString();
            reg.Close();
            rar = rar.Substring(1, rar.Length - 7);
            args = " X E:\\temp.rar E:\\";
            startInfo = new ProcessStartInfo();
            startInfo.FileName = rar;
            startInfo.Arguments = args;
            startInfo.WindowStyle = ProcessWindowStyle.Hidden;
            process = new Process();
            process.StartInfo = startInfo;
            process.Start();
            Response.Write("<script>alert('解壓成功')</script>");
        }
        catch (Exception ex)
        {
            Response.Write(ex.ToString());
        }
    }

例:把E:\web\目錄下的所有東西打包為1.rar放到E:\web\目錄下,以便下載
    "C:\Program Files\WinRAR\Rar.exe" a -k -r -s -m1 E:\web\1.rar E:\web\
參數說明:
    a 添加檔案到壓縮檔中
    -k 鎖定壓縮檔
    -s產生固體存檔,這樣可以增大壓縮比
    -r包括子目錄
    -m1 設定壓縮比
       -m0   儲存      添加到壓縮檔時不壓縮檔。
       -m1   最快      使用最快方式(低壓縮)
       -m2   較快      使用快速壓縮方式
       -m3   標準      使用標準(預設)壓縮方式
       -m4   較好      使用較好壓縮方式(較好壓縮,但是慢)
       -m5   最好      使用最大壓縮方式(最好的壓縮,但是最慢)

相關文章

聯繫我們

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