public class Unrar {
//服务器需要安装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;
}
//解压zip格式压缩包
private static void unzip(String sourceZip,String destDir) throws Exception{
try{
Project p = new Project();
Expand e = new Expand();
e.setProject(p);
e.setSrc(new File(sourceZip));
e.setOverwrite(false);
e.setDest(new File(destDir));
e.setEncoding("gbk");
e.execute();
}catch(Exception e){
throw e;
}
}
public static void main(String[] args) {
String rarFile= "D://a.rar";
String zipFile= "D://a.zip";
String rartarget= "D://123//";
String ziptarget= "D://456//";
unzip(zipFile, ziptarget);
boolean b = unrar(rarFile, rartarget);
System.out.println(b);
}
}
java实现解压rar5和zip
最新推荐文章于 2026-05-21 15:26:02 发布
本文介绍了一个Java程序,该程序使用WinRAR命令行工具解压RAR文件,并利用Apache Ant库解压ZIP文件。文章提供了完整的源代码示例,包括如何调用外部程序进行RAR文件解压以及如何使用Java实现ZIP文件解压。

1626

被折叠的 条评论
为什么被折叠?



