JAVA用命令方式解压rar格式压缩包

/**
     * 采用命令行方式解压文件
     * 所有文件才有绝对路径
     * @param rarFilePath 压缩文件路径+文件名
     * @param destDir     解压结果路径
     * @return
     */
    public static boolean unRARByCMD(String rarFilePath, String destDir,ActionErrors errors){
        boolean bool = false;
        File rarFile = new File(rarFilePath);
        if (!rarFile.exists()) {
            errors.add("errorMsg",new ActionError("error.userdefine","解压的rar文件不存在!"));
            return false;
        }
        File destDirPath = new File(destDir);
        if (!destDirPath.exists()) {
            destDirPath.mkdirs();
        }
        // 开始调用命令行解压,参数-o+是表示覆盖的意思
        String cmdPath = "C:\\Program Files\\WinRAR\\WinRAR.exe"; //windows中的路径
       //  String cmdPath = "/usr/local/bin/unrar"; 如果linux做了软连接 不需要这里配置路径
        String cmd = cmdPath + " X -o+ " + rarFile + " " + destDir;
        Process proc;
        try {
            proc = Runtime.getRuntime().exec(cmd);
            if (proc.waitFor() != 0) {
                if (proc.exitValue() == 0) {
                    bool = false;
                }
            } else {
                bool = true;
            }
        } catch (IOException e) {
            e.printStackTrace();
            errors.add("errorMsg",new ActionError("error.userdefine","RAR解压命令不正确!提示:"+e.getMessage()));
        } catch (InterruptedException e) {
            e.printStackTrace();
            errors.add("errorMsg",new ActionError("error.userdefine","RAR解压失败!提示:"+e.getMessage()));
        }
        return bool;
    }

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值