zoukankan      html  css  js  c++  java
  • Java代码中解压RAR文件

    [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

  • 相关阅读:
    java 随机流
    java 缓冲流
    java 文件字符输入、输出流
    【JMeter4.0】之 “jdk1.8、JMeter4.0” 安装与配置以及JMeter永久汉化和更改界面背景、并附加附录:个人学习总结
    【Selenium + Python】自动化测试之发送邮件正文以及附件同时发送
    【Mysql】之视图操作
    【Mysql】Navicat For Mysql快捷键
    【Axure插件】之浏览器打开失败
    【Mysql】之基础sql语句模板
    C语言中malloc、free和new、delete的用法和区别
  • 原文地址:https://www.cnblogs.com/wangfeng520/p/7054556.html
Copyright © 2011-2022 走看看