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. Making the Grade [POJ3666] [DP]

    题意: 给定一个序列,以最小代价将其变成单调不增或单调不减序列,代价为Σabs(i变化后-i变化前),序列长度<=2000,单个数字<=1e9 输入:(第一行表示序列长度,之后一行一个表示 ...

  2. day3字典_字符串_文件操作

    一.知识回顾 1.jmeter怎样增加压力机:在主控机配置文件 jmeter.perperties 文件中添加对应压力的IP和对应端口,多台压力机IP用","隔开,保证所有负载机和 ...

  3. helm-chart4,流程控制和变量

    控制结构(模板说法中称为"动作")提供了控制模板生成流程的能力.Helm的模板语言提供了以下控制结构: if/ else用于创建条件块 with 指定范围 range,它提供了一个 ...

  4. strtok strchr strrchr strchrnul

    NAME       strchr, strrchr, strchrnul - locate character in string SYNOPSIS       #include <strin ...

  5. ajax01

    ajax01 1.ajax简介 涉及AJAX的操作页面不能用文件协议访问 使用ajax发送请求: send参数缺省默认为null onreadyatatechange事件在状态改变时就会触发. .re ...

  6. Ajax发送请求,并接受字符串

    前台: $.ajax({ type: 'POST', url: url, dataType: "json", success : function(data){ alert(&qu ...

  7. MUI学习03-滚动图(幻灯片)及菜单项(九宫格)

    <!--标准mui.css-->引入CSS:<link rel="stylesheet" href="../css/mui.min.css"& ...

  8. 如何在MyBatis中优雅的使用枚举

    问题 在编码过程中,经常会遇到用某个数值来表示某种状态.类型或者阶段的情况,比如有这样一个枚举:   public enum ComputerState { OPEN(10), //开启 CLOSE( ...

  9. Creating a NuGet Package in 7 easy steps - Plus using NuGet to integrate ASP.NET MVC 3 into existing Web Forms applications

    UPDATE: Check out my follow up post where I remove the need for editing the Global.asax.cs and show ...

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

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