Asp.net實現線上壓縮與解壓

iDotNetSpace發表於2009-04-23

首先加入名稱空間

using Microsoft.Win32;
using System.Runtime.InteropServices;
using System.Diagnostics;
實現壓縮方法:


//壓縮
        String strRar;
        RegistryKey rkReg;
        Object obj;
        String strInfo;
        ProcessStartInfo psiInfo;
        Process pProcess;
        try
        {
            rkReg = Registry.ClassesRoot.OpenSubKey(@"Applications\WinRAR.exe\Shell\Open\Command");
            bj = rkReg.GetValue("");
            strRar = obj.ToString();
            rkReg.Close();
            strRar = strRar.Substring(1, strRar.Length - 7);
            strInfo = "a  -r  -ep1 test.rar " + lblTree.Text + @"rar/test1.txt " + lblTree.Text + @"rar";//這裡為rar的壓縮命令格式(也可以自行擴充套件)
            psiInfo = new ProcessStartInfo();
            psiInfo.FileName = strRar;
            psiInfo.Arguments = strInfo;
            psiInfo.WindowStyle. = ProcessWindowStyle.Hidden;
            psiInfo.WorkingDirectory = lblTree.Text + "rar"; ;//獲取或設定要啟動的程式的初始目錄。
            pProcess = new Process();
            pProcess.StartInfo = psiInfo;
            pProcess.Start();
            Response.Write("壓縮成功");
        }
        catch (Exception ex)
        {
            Response.Write(ex.ToString());
        }
實現解壓方法:

//解壓縮
        String strRar;
        RegistryKey rkReg;
        Object obj;
        String strInfo;
        ProcessStartInfo psiInfo;
        Process pProcess;
        try
        {
            rkReg = Registry.ClassesRoot.OpenSubKey(@"Applications\WinRar.exe\Shell\Open\Command");
            bj = rkReg.GetValue("");
            strRar = obj.ToString();
            rkReg.Close();
            strRar = strRar.Substring(1, strRar.Length - 7);
            strInfo = " X " + lblTree.Text + @"rar/test.rar " + " " + lblTree.Text + @"unRar";
            psiInfo = new ProcessStartInfo();
            psiInfo.FileName = strRar;
            psiInfo.Arguments = strInfo;
            psiInfo.WindowStyle. = ProcessWindowStyle.Hidden;
            pProcess = new Process();
            pProcess.StartInfo = psiInfo;
            pProcess.Start();
            Response.Write("解壓縮成功");
        }
        catch (Exception ex)
        {
            Response.Write(ex.ToString());
        }

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/12639172/viewspace-591573/,如需轉載,請註明出處,否則將追究法律責任。

相關文章