Java代码中解压RAR文件

  1. [java] view plaincopy
     
    1. import java.io.File;  
    2. import java.io.FileOutputStream;  
    3.   
    4. import de.innosystec.unrar.Archive;  
    5. import de.innosystec.unrar.rarfile.FileHeader;  
    6.   
    7. public class UnRARTools {  
    8.   
    9.     public void unrar(File sourceRar, File destDir) throws Exception {  
    10.         Archive archive = null;  
    11.         FileOutputStream fos = null;  
    12.         System.out.println("Starting...");  
    13.         try {  
    14.             archive = new Archive(sourceRar);  
    15.             FileHeader fh = archive.nextFileHeader();  
    16.             int count = 0;  
    17.             File destFileName = null;  
    18.             while (fh != null) {  
    19.                 System.out.println((++count) + ") " + fh.getFileNameString());  
    20.                 String compressFileName = fh.getFileNameString().trim();  
    21.                 destFileName = new File(destDir.getAbsolutePath() + "/" + compressFileName);  
    22.                 if (fh.isDirectory()) {  
    23.                     if (!destFileName.exists()) {  
    24.                         destFileName.mkdirs();  
    25.                     }  
    26.                     fh = archive.nextFileHeader();  
    27.                     continue;  
    28.                 }   
    29.                 if (!destFileName.getParentFile().exists()) {  
    30.                     destFileName.getParentFile().mkdirs();  
    31.                 }  
    32.                 fos = new FileOutputStream(destFileName);  
    33.                 archive.extractFile(fh, fos);  
    34.                 fos.close();  
    35.                 fos = null;  
    36.                 fh = archive.nextFileHeader();  
    37.             }  
    38.   
    39.             archive.close();  
    40.             archive = null;  
    41.             System.out.println("Finished !");  
    42.         } catch (Exception e) {  
    43.             throw e;  
    44.         } finally {  
    45.             if (fos != null) {  
    46.                 try {  
    47.                     fos.close();  
    48.                     fos = null;  
    49.                 } catch (Exception e) {  
    50.                     //ignore  
    51.                 }  
    52.             }  
    53.             if (archive != null) {  
    54.                 try {  
    55.                     archive.close();  
    56.                     archive = null;  
    57.                 } catch (Exception e) {  
    58.                     //ignore  
    59.                 }  
    60.             }  
    61.         }  
    62.     }  
    63.   
    64. }  

    需要引用到以下两个lib.
    java-unrar-0.5.jar
    http://www.java2s.com/Code/JarDownload/java/java-unrar-0.5.jar.zip
    apache-commons-logging.jar
    http://www.java2s.com/Code/JarDownload/apache-commons/apache-commons-logging.jar.zip

原文地址:https://www.cnblogs.com/yuanjun1/p/3929971.html