1.可以自己写一个java解压文件的程序
2.如果一定要调用bat文件的话可以这样
Runtime rt = Runtime.getRuntime();
Process pr = rt.exec("cmd /c dir");
类似这样
2.如果一定要调用bat文件的话可以这样
Runtime rt = Runtime.getRuntime();
Process pr = rt.exec("cmd /c dir");
类似这样
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.regex.Matcher;
import java.util.regex.Pattern;import de.innosystec.unrar.Archive;
import de.innosystec.unrar.rarfile.FileHeader;public class RarFileUtil {
public static boolean existZH(String str) {
String regEx = "[\\u4e00-\\u9fa5]";
Pattern p = Pattern.compile(regEx);
Matcher m = p.matcher(str);
return m.find();
} public static String getFileName(String fileName) {
if (null == fileName && "".equals(fileName)) {
return "";
}
int index = fileName.lastIndexOf(".");
return fileName.substring(0, index);
} public static boolean unrarFile(String rarFilePath, String extPlace) {
boolean flag = false;
if (null == rarFilePath || "".equals(rarFilePath.trim())) {
System.out.println("rar文件路径为空.");
return flag;
} else {
int index = rarFilePath.lastIndexOf(".");
if (index > -1) {
String suffix = rarFilePath.substring(index + 1,
rarFilePath.length());
if (!"rar".equalsIgnoreCase(suffix)) {
System.out.println("压缩文件格式不正确.");
return flag;
}
}
}
if (null == extPlace || "".equals(extPlace.trim())) {
System.out.println("解压路径为空.");
return flag;
} Archive archive = null;
OutputStream os = null; try {
File file = new File(rarFilePath);
String extDir = extPlace + getFileName(file.getName())
+ File.separator;
archive = new Archive(file);
FileHeader fh = archive.nextFileHeader(); String fileName = null;
String path = null;
String dirPath = null;
File dir = null;
while (null != fh) {
fileName = fh.getFileNameW().trim();
if (!existZH(fileName)) {
fileName = fh.getFileNameString();
} path = (extDir + fileName).replaceAll("\\\\", "/");
int end = path.lastIndexOf("/");
if (end > -1) {
dirPath = path.substring(0, end);
} dir = new File(dirPath);
if (!dir.exists()) {
dir.mkdirs();
} if (fh.isDirectory()) {
fh = archive.nextFileHeader();
continue;
} os = new FileOutputStream(extDir + fileName);
archive.extractFile(fh, os);
os.flush();
os.close(); fh = archive.nextFileHeader();
}
flag = true;
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != os) {
try {
os.flush();
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (null != archive) {
try {
archive.close();
} catch (IOException e) {
e.printStackTrace();
}
}
} return flag;
}
public static void main(String[] args){
unrarFile("E:\\新建文件夹.rar","E:\\");
}
}
简单的例子,不过例子有点问题,仅供参考
解决方法:
安装WinRAR,把安装路径写到环境变量里。在dos里输入WinRAR命令可以查看是否生效。楼上的叫人家用这个库那个工具的,都不认真看问题就回答的吗?
if (java.awt.Desktop.isDesktopSupported()) {
try {
java.awt.Desktop dp = java.awt.Desktop.getDesktop();
if (dp.isSupported(java.awt.Desktop.Action.BROWSE)) {
dp.open(new File("D:\\xxx.bat"));//把应用程序的地址传进来即中打开
}
} catch (java.lang.NullPointerException e) {
} catch (java.io.IOException e) {
}
}
}
"C:\Program Files\WinRAR\WinRAR.exe" e -y -inul "一键搞定.rar"