JAVA JDK中的解压缩似乎不能解压缩我们手工压缩的RAR文件,只能解压JDK中的API压缩的RAR
所以想出以下用WINRAR命令行方式的解压缩
package edu.nwpu.vcampus.util;
import java.io.File;
public class ZipUtil {
public static final String winrarPath = "C://Program Files//WinRAR//WinRAR.exe";
public static boolean unzip(String zipFile) {
boolean bool = false;
File f=new File(zipFile);
if(!f.exists())
{
return false;
}
String folder = f.getParent();
System.out.println(folder);
String cmd = winrarPath + " X " + zipFile + " "+folder;
//cmd="C://Program Files//WinRAR//WinRAR.exe X D://a.rar D://a";
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();
}
return bool;
}
public static boolean unzip(String zipFile,String password) {
boolean bool = false;
File f=new File(zipFile);
if(!f.exists())
{
return false;
}
String folder = f.getParent();
System.out.println(folder);
String cmd = winrarPath + " X -p " + password+" "+zipFile + " "+folder;
//cmd="C://Program Files//WinRAR//WinRAR.exe X D://a.rar D://a";
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();
}
return bool;
}
public static void main(String[] args) {
String zipFile = "D://a.rar";
boolean b = ZipUtil.unzip(zipFile);
System.out.println(b);
}
}