我正在寻找使用Java解压
目前我能够用不到20行代码提取
如果对任何人有帮助,这就是我用来提取
.rar
文件的方法,但无论我搜索哪里,都只能得到同一个工具 - JavaUnRar
。我一直在研究如何使用它来解压缩.rar
文件,但是我似乎找到的所有方法都很冗长且不方便,比如这个例子。目前我能够用不到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();
}
}