JAVA 实现最新的winrar 5 压缩格式的解压。
最近,听到一位朋友说,要实现JAVA 服务端解压rar 文件的需求。当然,想都没想,认为很简单的活,就干了。结果,悲具了。
大家在网上找到的 JAVA 实现rar 解压的,都是下面的方法(当然,这些方法都是无法完成 winrar 5 的解压的).
方式一 :JAVA 引用第三方包来解决。
<!-- 解压rar -->
<dependency>
<groupId>com.github.junrar</groupId>
<artifactId>junrar</artifactId>
<version>3.0.0</version>
</dependency>
悲具中,对于 winrar 5 最新的格式,无法解压,度娘中,得到如下结论:
方式二:引用winrar 软件的解压命令。
//服务器需要安装winrar
public static final String winrarPath = "C://Program Files//WinRAR//WinRAR.exe";
public static boolean unrar(String rarFile, String target) {
boolean bool = false;
File f=new File(rarFile);
if(!f.exists()){
return false;
}
String cmd = winrarPath + " X " + zipFile + " "+target;
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();
}
return bool;
}
悲具中,你当你们的服务器都是使用windows 操作系统的吗,使用linux 等其它的都是悲具中。
悲具中。。。。,想起鲁君的一句话,“不再悲具中爆发,就在悲具中灭亡。",
所以 悲具中爆发。下面是作者 编写的 解决 rar 5 解压的问题,支持跨平台(win,linux,mac,手机) 都通通没问题。
测试代码如下:
public static void main(String[] s) {
String rarFile = "/Users/happy/Desktop/test.rar";
String outDir = "/Users/happy/Desktop";
boolean b = false;
b = Rar.rar(rarFile, outDir);
System.out.println(b);
}
相关的实现代码,下载地址:
https://pan.baidu.com/s/1YwbQO8nYb2Pc6DNRbRWunQ