81,092
社区成员
发帖
与我相关
我的任务
分享
/**
* 将1个RAR文件解压
* rarFileName 需要解压的RAR文件(必须包含路径信息以及后缀)
* destDir 解压后的文件放置目录
*/
public static void unRARFile(String rarFileName, String destDir) {
if (!rarFileName.toLowerCase().endsWith(".rar")) {
System.out.println("非rar文件!");
return;
}
unrarCmd +=" "+ rarFileName + " " + destDir;
try {
Runtime rt = Runtime.getRuntime();
Process p = rt.exec(unrarCmd);
} catch (Exception e) {
System.out.println("e.getMessage()="+e.getMessage());
}
}
/**
* 根据winrar的文档推荐使用rar.exe, 另外加入参数-y在你需要选择的时候自动输入y
* 否则代码中要加入当winrar询问时的处理, 不然你的主进程将会被柱塞.
*/
private static String unrarCmd = "C:\\Program Files\\WinRAR\\rar.exe x -y";
/**
* 将1个RAR文件解压
* rarFileName 需要解压的RAR文件(必须包含路径信息以及后缀)
* destDir 解压后的文件放置目录
*/
public static void unRARFile(String rarFileName, String destDir) {
if (!rarFileName.toLowerCase().endsWith(".rar")) {
System.out.println("非rar文件!");
return;
}
unrarCmd +=" "+ rarFileName + " " + destDir;
Runtime rt = null;
Process p = null;
int result;
try {
rt = Runtime.getRuntime();
p = rt.exec(unrarCmd);
result = p.waitFor(); // 这一句将使你的当前主进程等待执行结束后才会继续执行后面代码.
/**
* 或者使用如下代码代替上面result = p.waitFor();
* InputStream in = null;
* InputStreamReader ir = null;
* BufferedReader reader = null;
* try {
* in = p.getInputStream();
* ir = new InputStreamReader(in, "gbk");
* //一般默认中文windows操作系统都是gbk字符集, 请根据你自己系统进行更改.
* reader = new BufferedReader(ir);
* String inStr = null;
* while ( (inStr = reader.readLine()) != null ) {
* System.out.println(inStr);
* }
* result = p.exitValue();
* } catch(Exception e) {
* e.printStackTrace();
* } finally {
* // 关闭之前的输入流什么的代码
* }
*/
if ( result != 0 ) {
System.out.println("解压失败");
}
} catch (Exception e) {
System.out.println("e.getMessage()="+e.getMessage());
}
}