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. MySql中的事务、JDBC事务、事务隔离级别

    一.MySql事务 之前在Oracle中已经学习过事务了,这个东西就是这个东西,但是在MySql中用法还是有一点不同,正好再次回顾一下. 先看看MySql中的事务,默认情况下,每执行一条SQL语句,都 ...

  2. yii2 basic版 MVC 部分

    一.model 1.什么是模型 模型是 MVC 模式中的一部分,是表现业务数据.规则和逻辑的对象 可通过继承 yii\base\Model 或它的子类(activeRecord)来定义模型类,基类 y ...

  3. Java 多线程 临界区

    临界区表示一种公共资源或共享数据,可以被多个线程使用.但是每一次只能有一个线程使用它.一旦临界区资源被占用,想使用该资源的其他线程必须等待. 例如,办公室里的打印机一次只能执行一个任务.当小王和小明同 ...

  4. python添加、修改、删除、访问类对象属性的2种方法

    1.直接添加.修改.删除.访问类对象属性 class Employee (object): empCount = 0 def __init__(self, name, salary) : self.n ...

  5. java基础知识总结--对象的克隆

    前提:在Java语言中所有的类的都是缺省的继承Java语言中的Object类的, protected native Object clone() throws CloneNotSupportedExc ...

  6. Kafka Streams简介: 让流处理变得更简单

    Introducing Kafka Streams: Stream Processing Made Simple 这是Jay Kreps在三月写的一篇文章,用来介绍Kafka Streams.当时Ka ...

  7. Win10+Ubuntu 二三事

    拯救者R720,反反复复弄了不少次,记录一下有用的blog 卸载 http://www.cnblogs.com/xia-Autumn/p/6294055.html https://blog.csdn. ...

  8. 用python实现ARP欺骗

    首先介绍一个python第三方库--Scapy,这个库不是标准库,默认是没有的,需要安装,不过在kali-linux里边是默认安装的, 这里我用kali做攻击者,xp做受害者 关于Scapy Scap ...

  9. java中常用的进制转换

    十进制转成十六进制:   Integer.toHexString(int i)   十进制转成八进制   Integer.toOctalString(int i)   十进制转成二进制   Integ ...

  10. django项目的新建相关的命令及配置

    创建工程 django-admin startproject 工程名称   运行开发服务器 python manage.py runserver   创建子应用 python manage.py st ...