com.github.junrar.exception.RarException: badRarArchive---rar5.0版解决方案

警告: exception in archive constructor maybe file is encrypted or currupt
com.github.junrar.exception.RarException: badRarArchive
出现这种问题,在网上查找资料,发现,rar解压分5.0之前和5.0之后的版本,5.0之前的版本,可以通过代码实现解压,WinRAR5之后,在rar格式的基础上,推出了另一种rar,叫RAR5,而java-unrar解析不了这种格式,目前官方也没有实现代码解压

解决方案:调用系统的rar解压工具解压

window 版本解压,可以使用WinRAR.exe文件解压

当然要先安装WinRAR软件,然后把WinRAR.exe拷贝到项目中,或者直接写WinRAR安装路径

/**
     * 采用命令行方式解压文件
     * @param zipFile 压缩文件
     * @param destDir 解压结果路径
     * @param cmdPath WinRAR.exe的路径,也可以在代码中写死
     * @return
     */
    public static boolean realExtract(File zipFile, String destDir,String cmdPath) {
        // 解决路径中存在/..格式的路径问题
        destDir = new File(destDir).getAbsoluteFile().getAbsolutePath();
        while(destDir.contains("..")) {
            String[] sepList = destDir.split("\\\\");
            destDir = "";
            for (int i = 0; i < sepList.length; i++) {
                if(!"..".equals(sepList[i]) && i < sepList.length -1 && "..".equals(sepList[i+1])) {
                    i++;
                } else {
                    destDir += sepList[i] + File.separator;
                }
            }
        }

        boolean bool = false;
        if (!zipFile.exists()) {
            return false;
        }

        // 开始调用命令行解压,参数-o+是表示覆盖的意思
        cmdPath = "E:\\workspace\\operation-uprr-manage\\src\\main\\resources\\cmd\\WinRAR.exe";
        String cmd = cmdPath + " X -o+ " + zipFile + " " + destDir;
        System.out.println(cmd);
        try {
            Process proc = Runtime.getRuntime().exec(cmd);
            if (proc.waitFor() != 0) {
                if (proc.exitValue() == 0) {
                    bool = false;
                }
            } else {
                bool = true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("解压" + (bool ? "成功" : "失败"));
        return bool;
    }
Linux版本解压,先安装unrar,然后调用unrar命令解压

1.安装unrar
wget https://www.rarlab.com/rar/rarlinux-x64-5.7.0.tar.gz 下载unrar包
tar -zxf rarlinux-x64-5.7.0.tar.gz
cd rar
make
make install
安装完成unrar,安装默认路径是/usr/local/bin
里面有rar 和 unrar可执行文件
/usr/local/bin/unrar,路径写这个路径即可,调用上述方法

  • 9
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值