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. BZOJ2042 : [2009国家集训队]Will的烦恼

    不难发现题中过程对应着动态维护关于$C$的最大生成树. 为了让$D$最大,同时让字典序最大,那么最后得到的一定是按$pair(C,D,编号)$排序的最大生成树. 对于每条非树边$(u,v,C)$,那么 ...

  2. Incorrect username or password ( access token )解决

    Q:Git提交时,给出提示Incorrect username or password ( access token ) K: 此处是用户名或者密码有误,建议解决方法两种.具体看哪一种可行,可试. 第 ...

  3. Spring 4 MVC example with Maven - [Source Code Download]

    In this tutorial, we show you a Spring 4 MVC example, using Maven build tool. Technologies used : Sp ...

  4. python之进程和线程3

    1 multiprocessing模块 (1.)直接导入 from multiprocessing import Process import os import time def info(name ...

  5. JDBC(11)—数据库连接池

    在实际开发过程中,特别是在web应用系统中,如果程序直接访问数据库中的数据,每一次数据访问请求丢必须经历建立数据库连接.打开数据库.存取数据和关闭数据库连接.而连接并打开数据库是一件既消费资源又费时的 ...

  6. IE环境表单提交不跳转页面

    <head> <base target="_self"></base> </head>

  7. windows10创建ftp服务器

    1.创建用户 2.创建FTP服务 3.开通防火墙服务 建立端口21,20入站规则 4.访问测试

  8. 【Spring】bean动态注册到spring

    /* * http://412887952-qq-com.iteye.com/blog/2348445 * http://www.jb51.net/article/106558.htm * https ...

  9. phpt5支付宝登陆支付接口解析

    先看效果图 下面的源码来源网络,自己对照修改. 放入一个插件库中,方便管理 创建支付类 1.发起支付 public function init() { $order_id = $_REQUEST['o ...

  10. SSE图像算法优化系列九:灵活运用SIMD指令16倍提升Sobel边缘检测的速度(4000*3000的24位图像时间由480ms降低到30ms)。

    这半年多时间,基本都在折腾一些基本的优化,有很多都是十几年前的技术了,从随大流的角度来考虑,研究这些东西在很多人看来是浪费时间了,即不能赚钱,也对工作能力提升无啥帮助.可我觉得人类所谓的幸福,可以分为 ...