Java junrar只能解压rar5.0一下的rar压缩文件,解压rar5.0以上文件需要调用UnRAR.exe程序进行解压。
import java.io.BufferedReader;
import java.io.InputStreamReader;
/**
* 解压rar压缩文件
* @author Administrator
*
*/
public class UnRar {
//获取项目路径
private static String property = System.getProperty("user.dir");
/**
* rar解压
* @param targetPath 解压文件路径
* @param absolutePath 解压后存放的目录
*/
public static String unRarFile(String uncompressFile, String targetPath) {
try {
long start = System.currentTimeMillis();
// 项目下UnRAR解压程序的路径
String cmd = property+"/unrar/UnRAR.exe";
//cmd命令
String unrarCmd = cmd + " x -r -p- -o+ " + uncompressFile + " "
+ targetPath;
Runtime rt = Runtime.getRuntime();
//调用程序UnRAR解压程序(Windows)
Process pre = rt.exec(unrarCmd);
//防止文件乱码
InputStreamReader isr = new InputStreamReader(pre.getInputStream(),"GBK");
BufferedReader bf = new BufferedReader(isr);
String line = null;
while ((line = bf.readLine()) != null) {
line = line.trim();
if ("".equals(line)) {
continue;
}
System.out.println(line);
}
bf.close();
pre.destroy();
long end = System.currentTimeMillis();
System.out.println("解压完毕,消时:"+(end-start)*0.001+"秒");
} catch (Exception e) {
System.out.println("解压发生异常");
}
return targetPath+"/";
}
}