【发布时间】:2012-07-23 17:52:34
【问题描述】:
我正在寻找一种使用 Java 解压缩 .rar 文件的方法,并且无论我在哪里搜索,我都会一直使用相同的工具 - JavaUnRar。我一直在研究用这个解压缩 .rar 文件,但我似乎找到的所有方法都非常漫长和尴尬,就像在 this example 中一样。
我目前能够在 20 行或更少的代码中提取 .tar、.tar.gz、.zip 和 .jar 文件,因此必须有一种更简单的方法来提取 .rar 文件,有人知道吗?
如果它可以帮助任何人,这是我用来提取 .zip 和 .jar 文件的代码,它适用于两者
public void getZipFiles(String zipFile, String destFolder) throws IOException {
BufferedOutputStream dest = null;
ZipInputStream zis = new ZipInputStream(
new BufferedInputStream(
new FileInputStream(zipFile)));
ZipEntry entry;
while (( entry = zis.getNextEntry() ) != null) {
System.out.println( "Extracting: " + entry.getName() );
int count;
byte data[] = new byte[BUFFER];
if (entry.isDirectory()) {
new File( destFolder + "/" + entry.getName() ).mkdirs();
continue;
} else {
int di = entry.getName().lastIndexOf( '/' );
if (di != -1) {
new File( destFolder + "/" + entry.getName()
.substring( 0, di ) ).mkdirs();
}
}
FileOutputStream fos = new FileOutputStream( destFolder + "/"
+ entry.getName() );
dest = new BufferedOutputStream( fos );
while (( count = zis.read( data ) ) != -1)
dest.write( data, 0, count );
dest.flush();
dest.close();
}
}
【问题讨论】:
-
通过各种压缩文件,我假设您在 linux 环境中。为什么不从 Java 调用 shell 命令。它将更加分拣和更快。
-
不,我实际上正在使用 Windows,但我目前正在处理的应用程序具有能够解压缩 .tar.gz 文件的规范,所以我必须这样做......我希望它是一个独立的应用程序,所以如果我能提供帮助,我真的不想在应用程序之外进行调用