java winrar_Java使用winrar解压缩 | 学步园

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);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值