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;
    }


版权声明:本文为cxws110原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。