java 解压文件的问题

zhuyc 2014-07-14 08:35:48
有一个内有一千个文件的压缩包,通过以下代码解压后只有43个文件!
public static boolean unRARFile(String rarFileName, String destDir) {
String unrarCmd = "C:\\Program Files (x86)\\WinRAR\\UnRar x ";
unrarCmd += rarFileName + " " + destDir;
try {
Runtime rt = Runtime.getRuntime();
Process p = rt.exec(unrarCmd);
return true;
} catch (Exception e) {
e.printStackTrace();
}
return false;
}

通过下面的代码,也是能解压出43个文件,但是线程一直在等待,但是强制停止后,所有文件就加压出来了,这是什么原因啊?
public static void main(String[] args) {
String rarFileName = "d:\\Users\\Desktop\\1\\ReadRQ.rar";
String destDir = "d:\\Users\\Desktop\\1\\";
String unrarCmd = "C:\\Program Files (x86)\\WinRAR\\UnRar x ";
unrarCmd += rarFileName + " " + destDir;
Runtime rt = null;;
Process p = null;
try {
rt = Runtime.getRuntime();
p = rt.exec(unrarCmd);
int exitVal = p.waitFor();
if (exitVal == 0) {
System.out.println("解压成功!");
}
} catch (Exception e) {
System.out.println(new Date() + ",解压失败:" + e.getMessage());
} finally {
p.destroy();
rt.freeMemory();
}

}
...全文
168 3 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
vnvlyp 2014-07-15
  • 打赏
  • 举报
回复
Rar有第三方库吧,比如Java unrar。。。不一定要靠调用本地命令行来解压吧
  • 打赏
  • 举报
回复
public static void unzip(String zipFilePath, String unzipDirectory) throws Exception { // 创建文件对象 File file = new File(zipFilePath); // 创建zip文件对象 ZipFile zipFile = new ZipFile(file); // 创建本zip文件解压目录 File unzipFile = new File(unzipDirectory + "/" + getSuffixName(file.getName())); if (unzipFile.exists()) unzipFile.delete(); unzipFile.mkdir(); // 得到zip文件条目枚举对象 Enumeration zipEnum = zipFile.getEntries(); // 定义输入输出流对象 InputStream input = null; OutputStream output = null; // 定义对象 ZipEntry entry = null; // 循环读取条目 while (zipEnum.hasMoreElements()) { // 得到当前条目 entry = (ZipEntry) zipEnum.nextElement(); String entryName = new String(entry.getName()); // 用/分隔条目名称 String names[] = entryName.split("\\/"); int length = names.length; String path = unzipFile.getAbsolutePath(); for (int v = 0; v < length; v++) { if (v < length - 1) { // 最后一个目录之前的目录 path += "/" + names[v] + "/"; createDir(path); } else { // 最后一个 if (entryName.endsWith("/")) // 为目录,则创建文件夹 createDir(unzipFile.getAbsolutePath() + "/" + entryName); else { // 为文件,则输出到文件 input = zipFile.getInputStream(entry); output = new FileOutputStream(new File( unzipFile.getAbsolutePath() + "/" + entryName)); byte[] buffer = new byte[1024 * 8]; int readLen = 0; while ((readLen = input.read(buffer, 0, 1024 * 8)) != -1) output.write(buffer, 0, readLen); // 关闭流 input.close(); output.flush(); output.close(); } } } } }
baidu_17686895 2014-07-15
  • 打赏
  • 举报
回复
- -同等~

81,122

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧