java实现rar解压(windows/linux)

一、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.cn/soft/5.htm
三、linux系统,要安装
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,路径写这个路径即可,调用上述方法

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值