用java提取.rar文件

时间:2012-07-25 10:06:38

标签: java extract rar

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

一样

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

如果它帮助任何人这是我用来提取.rar.zip文件的代码,那么它适用于

.jar

4 个答案:

答案 0 :(得分:18)

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

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

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

您可以在问题RAR archives with java中找到对其他Java RAR库的一些引用。另外,问题How to compress text file to rar format using java program更多地解释了不同的解决方法(例如,使用Runtime)。

答案 1 :(得分:1)

您可以使用库junrar

<dependency>
   <groupId>com.github.junrar</groupId>
   <artifactId>junrar</artifactId>
   <version>0.7</version>
</dependency>

代码示例:

            File f = new File(filename);
            Archive archive = new Archive(f);
            archive.getMainHeader().print();
            FileHeader fh = archive.nextFileHeader();
            while(fh!=null){        
                    File fileEntry = new File(fh.getFileNameString().trim());
                    System.out.println(fileEntry.getAbsolutePath());
                    FileOutputStream os = new FileOutputStream(fileEntry);
                    archive.extractFile(fh, os);
                    os.close();
                    fh=archive.nextFileHeader();
            }

答案 2 :(得分:0)

你可以简单地将这个maven依赖项添加到你的代码中:

<dependency>
    <groupId>com.github.junrar</groupId>
    <artifactId>junrar</artifactId>
    <version>0.7</version>
</dependency>

然后使用此代码提取rar文件:

        File rar = new File("path_to_rar_file.rar");
    File tmpDir = File.createTempFile("bip.",".unrar");
    if(!(tmpDir.delete())){
        throw new IOException("Could not delete temp file: " + tmpDir.getAbsolutePath());
    }
    if(!(tmpDir.mkdir())){
        throw new IOException("Could not create temp directory: " + tmpDir.getAbsolutePath());
    }
    System.out.println("tmpDir="+tmpDir.getAbsolutePath());
    ExtractArchive extractArchive = new ExtractArchive();
    extractArchive.extractArchive(rar, tmpDir);
    System.out.println("finished.");

答案 3 :(得分:0)

您可以使用http://sevenzipjbind.sourceforge.net/index.html

除了支持多种存档格式外,版本16.02-2.01还完全支持通过以下方式提取RAR5:

  • 受密码保护的档案文件
  • 带有加密标题的档案
  • 按数量划分的归档文件

渐变

implementation 'net.sf.sevenzipjbinding:sevenzipjbinding:16.02-2.01'
implementation 'net.sf.sevenzipjbinding:sevenzipjbinding-all-platforms:16.02-2.01'

或行家

<dependency>
    <groupId>net.sf.sevenzipjbinding</groupId>
    <artifactId>sevenzipjbinding</artifactId>
    <version>16.02-2.01</version>
</dependency>
<dependency>
    <groupId>net.sf.sevenzipjbinding</groupId>
    <artifactId>sevenzipjbinding-all-platforms</artifactId>
    <version>16.02-2.01</version>
</dependency>

和代码示例


import net.sf.sevenzipjbinding.ExtractOperationResult;
import net.sf.sevenzipjbinding.IInArchive;
import net.sf.sevenzipjbinding.SevenZip;
import net.sf.sevenzipjbinding.impl.RandomAccessFileInStream;
import net.sf.sevenzipjbinding.simple.ISimpleInArchiveItem;

import java.io.*;
import java.util.HashMap;
import java.util.Map;

/**
 * Responsible for unpacking archives with the RAR extension.
 * Support Rar4, Rar4 with password, Rar5, Rar5 with password.
 * Determines the type of archive itself.
 */
public class RarExtractor {

    /**
     * Extracts files from archive. Archive can be encrypted with password
     *
     * @param filePath path to .rar file
     * @param password string password for archive
     * @return map of extracted file with file name
     * @throws IOException
     */
    public Map<InputStream, String> extract(String filePath, String password) throws IOException {
        Map<InputStream, String> extractedMap = new HashMap<>();

        RandomAccessFile randomAccessFile = new RandomAccessFile(filePath, "r");
        RandomAccessFileInStream randomAccessFileStream = new RandomAccessFileInStream(randomAccessFile);
        IInArchive inArchive = SevenZip.openInArchive(null, randomAccessFileStream);

        for (ISimpleInArchiveItem item : inArchive.getSimpleInterface().getArchiveItems()) {
            if (!item.isFolder()) {
                ExtractOperationResult result = item.extractSlow(data -> {
                    extractedMap.put(new BufferedInputStream(new ByteArrayInputStream(data)), item.getPath());

                    return data.length;
                }, password);

                if (result != ExtractOperationResult.OK) {
                    throw new RuntimeException(
                            String.format("Error extracting archive. Extracting error: %s", result));
                }
            }
        }

        return extractedMap;
    }
}

P.S。 @BorisBrodski https://github.com/borisbrodski祝您40岁生日快乐!希望你有一个伟大的庆祝活动。谢谢您的工作!