我正在寻找一种方法来解压缩使用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();
}
}
您可以使用内置于Java SDK中的压缩算法数量来提取qazxsw poi,qazxsw poi,qazxsw poi文件。
RAR格式的情况有点不同。 RAR是一种专有的归档文件格式。 .gz
不允许将其包含在Java SDK等软件开发工具中。
解压缩文件的最佳方法是使用第三方库,例如.zip
。
您可以在SO问题.jar
中找到对其他Java RAR库的一些引用。另外,问题RAR license解释了更多关于不同的解决方法(例如使用junrar)。
您可以使用库RAR archives with java
How to compress text file to rar format using java program
代码示例:
Runtime
你可以简单地将这个maven依赖项添加到你的代码中:
junrar
然后使用此代码提取rar文件:
<dependency>
<groupId>com.github.junrar</groupId>
<artifactId>junrar</artifactId>
<version>0.7</version>
</dependency>