当前位置: 首页 > news >正文

Java 解压 rar 文件

1、引入依赖

<!--      解压rar5,所需依赖开始-->
        <dependency>
            <groupId>com.github.axet</groupId>
            <artifactId>java-unrar</artifactId>
            <version>1.7.0-8</version>
        </dependency>
        <dependency>
            <groupId>net.sf.sevenzipjbinding</groupId>
            <artifactId>sevenzipjbinding</artifactId>
            <version>16.02-2.01</version>
        </dependency>
        <dependency>
            <groupId>net.sf.sevenzipjbinding</groupId>
            <artifactId>sevenzipjbinding-all-platforms</artifactId>
            <version>16.02-2.01</version>
        </dependency>
        <!--      解压rar5,所需依赖结束-->

2、解压方法代码

public class FileUtils {

    public static List<String> unRar(String rarPath, String dstDirectoryPath) throws IOException {
        IInArchive archive;
        RandomAccessFile randomAccessFile = new RandomAccessFile(rarPath, "r");
        ;
        // 第一个参数是需要解压的压缩包路径,第二个参数参考JdkAPI文档的RandomAccessFile
        // r代表以只读的方式打开文本,也就意味着不能用write来操作文件
        archive = SevenZip.openInArchive(null,
                new RandomAccessFileInStream(randomAccessFile));
        int[] in = new int[archive.getNumberOfItems()];
        for (int i = 0; i < in.length; i++) {
            in[i] = i;
        }
        archive.extract(in, false, new ExtractCallback(archive, dstDirectoryPath));
        archive.close();
        randomAccessFile.close();
        log.info("rar文件电子回单解压目标文件夹为:{}", dstDirectoryPath);
        return null;
    }

    /**
     * 获取路径下的所有文件/文件夹
     * @param directoryPath 需要遍历的文件夹路径
     * @param isAddDirectory 是否将子文件夹的路径也添加到list集合中
     * @return
     */
    public static List<String> getAllFile(String directoryPath, boolean isAddDirectory) {
        List<String> list = new ArrayList<>();
        File baseFile = new File(directoryPath);
        if (baseFile.isFile() || !baseFile.exists()) {
            return list;
        }
        File[] files = baseFile.listFiles();
        if (ArrayUtil.isEmpty(files)) {
            return new ArrayList<>(0);
        }
        for (File file : files) {
            if (file.isDirectory()) {
                if (isAddDirectory) {
                    list.add(file.getAbsolutePath());
                }
                list.addAll(getAllFile(file.getAbsolutePath(), isAddDirectory));
            } else {
                list.add(file.getAbsolutePath());
            }
        }
        return list;
    }


}

3、将解压方法封装到工具类中

// 解压 rar 文件
if (folderFile.getName().endsWith(".rar") || folderFile.getName().endsWith(".RAR")) {
    try {
        FileUtils.unRar(terminalFilePath + "/" + folderFile.getName(), terminalFilePath);
    } catch (Exception e) {
        throw new DataHandleException("rar 文件解压失败");
    }
}
http://www.dtcms.com/a/125620.html

相关文章:

  • CSS属性书写顺序
  • wireshark过滤器表达式的规则
  • linux 定时器管理系统设计与实现
  • MySql主从相关概念
  • windows虚拟内存
  • 全局异常处理器的基本使用
  • JS-防抖与节流
  • 【STM32】解读启动文件startup_stm32f10x_md.s
  • Vue 大文件分片上传组件实现解析
  • 互联网三高-高性能之无锁编程
  • [蓝桥杯 2023 省 A] 平方差
  • CSS高度坍塌?如何解决?
  • 达梦数据库-学习-16-常用SQL记录(持续更新)
  • 【家政平台开发(36)】数据迁移与初始化开发:筑牢家政平台的数据根基
  • JavaScript 代码混淆与反混淆技术详解
  • 构建高可靠C++服务框架:从日志系统到任务调度器的完整实现
  • 定制一款国密浏览器(5):修改浏览器名称
  • Python 关键字详解
  • 超低功耗MCU软件开发设计中的要点与选型推荐
  • 基于SSM的线上花店鲜花销售商城网站系统
  • spark-core编程2
  • 使用Python计算万有引力势能
  • MYOJ_4553:(洛谷P1022)[NOIP 2000 普及组] 计算器的改良(数学运算与求解相关)
  • ubuntu22.04下安装mysql以及mysql-workbench
  • 【2025年认证杯数学中国数学建模网络挑战赛】A题解题思路与模型代码
  • ssh 登录报错集合(FQA)
  • [WUSTCTF2020]level1
  • 198. 打家劫舍:动态规划
  • Unifying Short and Long-Term Tracking with Graph Hierarchies—CVPR2023
  • Spring定时任务修仙指南:从@Scheduled到分布式调度的终极奥义