关于解压RAR(JAVA&C#)

一、JAVA解压RAR
 
    由于JAVA中无直接对RAR操作的类,则借助命令提示符功能进行操作解压。
    //compress 源文件路径
    //decompression 目标文件路径
 
java.lang.Runtime rt = java.lang.Runtime.getRuntime();
    Process p = rt.exec("C:\\Program Files\\WinRAR\\UNRAR.EXE x -o+ -p- " + compress + " " + decompression);    //for Win
    Process p = rt.exec("unrar x -o- -y " + compress + " " + decompression + "/");        //for Linux
    StringBuffer sb = new StringBuffer();
    java.io.InputStream fis = p.getInputStream();
    int value = 0;
    while ((value = fis.read()) != -1)
      {
              sb.append((char) value);
      }
   fis.close();
   String result = new String(sb.toString().getBytes("ISO-8859-1"), "GBK");
   System.out.println(result);

二、C#解压 RAR

 /**
         *    解压RAR
         *    path  :目标文件夹;    rarPath :源文件路径
         **/ 
        public bool UnRAR(string path, string rarPath)   
        {   
            bool flag = false;   
            string rarexe;   
            RegistryKey regkey;   
            Object regvalue;   
            string cmd;   
            ProcessStartInfo startinfo;   
            Process process;   
            try  
            {

                //从注册表获取winrar.exe文件路径
                regkey = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\App Paths\\WinRAR.exe"); 
                regvalue = regkey.GetValue("");   
                rarexe = regvalue.ToString();   
                regkey.Close();

                if (Directory.Exists(path) == false)
                {
                    Directory.CreateDirectory(path);
                }    

                //命令提示句

                cmd = "x -o- -y " + rarPath + " " + path;
                startinfo = new ProcessStartInfo();   
                startinfo.FileName = rarexe;   
                startinfo.Arguments = cmd;   
                startinfo.WindowStyle = ProcessWindowStyle.Normal;     
                process = new Process();   
                process.StartInfo = startinfo;   
                process.Start();   
                process.WaitForExit();   
                if (process.HasExited)   
                {   
                    flag = true;   
                }   
                process.Close();   
            }   
            catch (Exception e)   
            {   
                throw e;   
            }   
            return flag;   
        }

 

posted @ 2013-09-29 11:31  Light Xun  阅读(233)  评论(0)    收藏  举报