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

需要引用到以下两个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代码中解压RAR文件的更多相关文章

  1. linux中解压rar文件

    linux平台默认是不支持RAR文件的解压,需要安装linux版本的RAR压缩软件,下载地址为:http://www.rarlab.com/download.htm 下载之后进行解压之后,进入rar目 ...

  2. 如何在linux中解压.rar文件

    在liunx下原本是不支持rar文件的,需要安装liunx下的winrar版本 步骤: 1.http://www.rarsoft.com/rar/rarlinux-4.0.1.tar.gz 从这个网址 ...

  3. CentOS解压rar文件

    默认不能解压rar文件. 进官网下载:http://www.rarsoft.com/download.htm RAR 5.40 for Linux x64 安装: # tar -zxvf rarlin ...

  4. Linux解压rar文件

    Linux解压rar文件(unrar安装和使用,分卷解压) windows平台很多压缩文档为rar文件,那么怎么做到Linux解压rar文件(unrar安装和使用)? 简单,centos5安装unra ...

  5. python循环解压rar文件

    python循环解压rar文件 C:. │ main.py │ ├─1_STL_算法简介 │ STL_算法简介.rar │ └─2_STL_算法_填充新值 STL_算法_填充新值.rar 事情是这样的 ...

  6. ubuntu解压rar文件

    一般通过默认安装的ubuntu是不能解压rar文件的,只有在安装了rar解压工具之后,才可以解压.其实在ubuntu下安装rar解压工具是非常简 单的,只需要两个步骤就可以迅速搞定. ubuntu 下 ...

  7. rar x 解压rar文件,提示permission denied

    问题: 解压rar文件,提示

  8. java 提取(解压)rar文件中特定后缀的文件并保存到指定目录

    内容简介 本文主要介绍使用junrar来提取rar压缩文件中特定后缀(如:png,jpg)的文件并保存到指定目录下. 支持v4及以下版本压缩文件,不支持v5及以上. 在rar文件上右键,查看属性,在压 ...

  9. 在Ubuntu系统中解压rar和zip文件的方法

    大家在以前的windows系统中会存有很多rar和zip格式的压缩文件,Ubuntu系统默认情况下对这些文件的支持不是很好,如果直接用"归档管理器"打开会提示错误,因此今天跟大家分 ...

随机推荐

  1. BZOJ1889 : Maximal

    二分答案,判断是否存在合法方案使得每个数都不超过$mid$. 考虑网络流建图: $i$点的流量下限为$\max(a_i-mid,0)$,费用为$1$,故拆点进行限制. $i$向$i+1$.$S$向$i ...

  2. [P1441]砝码称重 (搜索+DP)

    对于我这种蒟蒻,是很不错的一题了. dfs搜索当前状态 满足时DP 比较坑的地方就是起始的地方 我一开始从1开始,搜索写的是从0开始. 后来就统一用0开始的了. #include<bits/st ...

  3. C#数组,ArrayList,List

    一.数组声明方式 1,声明一个未经初始化的数组引用,以后可以把这引用初使化为一个数组实例 int[] int_array; int_array = new int[2]; 注:数组的引用必须以相同或相 ...

  4. JAVA自学笔记07

    JAVA自学笔记07 1.构造方法 1) 例如:Student s = new Student();//构造方法 System.out.println(s);// Student@e5bbd6 2)功 ...

  5. R12.2.4 ORA-01017: invalid username/password; logon denied

    sqlplus / as sysdba ORA-01017: invalid username/password; logon denied [oracle@ebs 11.2.0]$ source / ...

  6. arcgis 获得工具箱工具的个数

    import arcgisscripting import string; gp = arcgisscripting.create(9.3); ##多少个工具箱 toolboxes = gp.list ...

  7. .Net转Java.02.数据类型

    .NET中常见的数据类型分类分别是值类型和引用类型 值类型包括(基元类型.struct.枚举) 引用类型包括(类.类.数组.接口.指针) Java分为,基本类型和类   C#   Java   值类型 ...

  8. 表型数据(Phenotype Data)基本概念

    表型(英语:Phenotype),又称表现型,对于一个生物而言,表示它某一特定的物理外观或成分.一个人是否有耳珠.植物的高度.人的血型.蛾的颜色等等,都是表型的例子. 表型主要受生物的基因型和环境影响 ...

  9. iOS:如何实现在文字上添加拼音

    一.介绍 最近项目有一个需求,需要给朗诵的文字添加对应的拼音,而且要求使用原生的控件实现.一开始听到这个需求挺懵逼的,感觉有点难.后来,静下来想一下,其实还是可以实现的,无非就是自定义了.下面,就来说 ...

  10. 如何做好Puppet Modules管理

    如何做好Puppet Modules管理 不同于其他的Openstack项目,puppet modules是一个数量庞大的存在.以我们当前在使用中的puppet modules为例,就已经多达96个( ...