/**
* 采用命令行方式解压文件
* 所有文件才有绝对路径
* @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;
}
JAVA用命令方式解压rar格式压缩包
最新推荐文章于 2023-11-04 14:55:03 发布