admin管理员组文章数量:1632155
一、package com.linf.spb004thymeleaf.newtest;
import java.io.File;
public class Test4 {
/**
* 采用命令行方式解压文件
* @param zipFile 压缩文件
* @param destDir 解压结果路径
* @param cmdPath WinRAR.exe的路径,也可以在代码中写死
* @return
*/
public static void main(String[] args) {
File f = new File(“D:\rarceshi\课改系统20200917.rar”);
realExtract(f,“D:\rarceshi\”,“D:\Program Files (x86)\WinRAR.exe”);
}
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;
}
}
二、windows系统,要安装winRar软件,地址:
http://www.51xiazai/soft/5.htm
三、linux系统,要安装
Linux版本解压,先安装unrar,然后调用unrar命令解压
1.安装unrar
wget https://www.rarlab/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,路径写这个路径即可,调用上述方法
版权声明:本文标题:java实现rar解压(windowslinux) 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://www.elefans.com/dianzi/1729114442a1187148.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论