用java提取.rar文件 [英] Using java to extract .rar files

查看:155
本文介绍了用java提取.rar文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种方法来解压缩 .rar 使用Java的文件以及我在哪里搜索我最后使用相同的工具 - JavaUnRar 。我一直在寻找解压缩 .rar 这样的文件,但我似乎发现这样做的所有方式都非常长而且很尴尬,就像在这个例子

I am looking a ways to unzip .rar files using Java and where ever I search i keep ending up with the same tool - JavaUnRar. I have been looking into unzipping .rar files with this but all the ways i seem to find to do this are very long and awkward like in this example

我目前能够提取 .tar .tar.gz .zip .jar 20行代码或更少的文件,因此必须有一种更简单的方法来提取 .rar 文件,有人知道吗?

I am currently able to extract .tar, .tar.gz, .zip and .jar files in 20 lines of code or less so there must be a simpler way to extract .rar files, does anybody know?

如果它对任何人有帮助这就是我用来代码提取 .zip .jar 文件,它适用于两者

Just if it helps anybody this is the code that I am using to extract both .zip and .jar files, it works for both

 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();
    }
}


推荐答案

你能够提取 .gz .zip .jar 文件,因为它们使用Java SDK中内置的压缩​​算法的数量。

You are able to extract .gz, .zip, .jar files as they use number of compression algorithms built into the Java SDK.

RAR 格式的情况略有不同。 RAR是专有归档文件格式。 RAR许可不允许将其包含在Java SDK等软件开发工具中。

The case with RAR format is a bit different. RAR is a proprietary archive file format. RAR license does not allow to include it into software development tools like Java SDK.

unrar 文件的最佳方法是使用第三方库,例如 junrar

The best way to unrar your files will be using 3rd party libraries such as junrar.

您可以在SO问题中找到对其他Java RAR库的一些引用使用java进行ROM存档。另外问题如何使用java程序将文本文件压缩为rar格式解释了不同的解决方法(例如使用运行时)。

You can find some references to other Java RAR libraries in SO question RAR archives with java. Also SO question How to compress text file to rar format using java program explains more on different workarounds (e.g. using Runtime).

这篇关于用java提取.rar文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆