java如何解压缩rar文件?

解决方案 »

  1.   

    // Decompress a zip file to a specific dir
       public String doUnzip(String sourceFileName, String targetFolderName) throws FileNotFoundException, IOException
       {
             String targetName = null;
             if (!targetFolderName.endsWith("/") || !targetFolderName.endsWith("\\"))
             {
                   targetName = targetFolderName.replace('\\', '/');
                   targetName = targetFolderName + "/" +"temp" +"/" ;
                   System.out.println("File targetName is "+targetName);
             }         sourceFileName = sourceFileName.replace('\\', '/');         try
             {
                   File tmpDirFile = new File(targetName);
                   if (!tmpDirFile.exists())
                      tmpDirFile.mkdirs();               ZipFile zipFile = new ZipFile(sourceFileName);
                   ZipEntry zipEntry = null;               byte[] buffer = new byte[4096];
                   int bytesRead = 0;               for (Enumeration fileLists = zipFile.entries(); fileLists.hasMoreElements(); )
                   {
                     String fileName = fileLists.nextElement().toString();
                    // System.out.println("Element is " + fileName);
                     File scriptFile = new File(targetName + fileName);                 if (!fileName.endsWith("/"))
                     {
                             scriptFile.getParentFile().mkdirs();
                             System.out.println("UNZIP: " + scriptFile.getPath());                         zipEntry = new ZipEntry(fileName);                         BufferedInputStream bufferFileIn = new BufferedInputStream(zipFile.getInputStream(zipEntry));
                             FileOutputStream outputStream = new FileOutputStream(scriptFile);                        while ( (bytesRead = bufferFileIn.read(buffer)) > 0)
                                   outputStream.write(buffer, 0, bytesRead);                        bufferFileIn.close();
                            outputStream.close();
                     }               }
                   zipFile.close();             }
                 catch(IOException ioErr)
                 {
                     System.out.println("Decompress file fail");
                     ioErr.printStackTrace();
                 }        //look tcl file and write suite file
               return targetName;   }
      

  2.   

    JAVA中,只能创建一个zip压缩文件。
    如果是rar的话,好像没有这个api。
    另外人家rar的算法也不会公开出来的
      

  3.   

    其实就用zip多好呀,
    还支持linux