Java解压缩rar文件

之前工作中遇到一个需求:rar 文件的压缩与解压缩。但是因为需求变动,还没来得及写,最近得空调研一下,发现 rar 比 zip 处理麻烦很多,这里小小记录一下。

rar是个专有格式,rar许可证不允许软件开发工具如JDK集成它,如果要在Java中解压rar文件,只能利用第三方工具依赖,如 junrar。官方地址为:GitHub - junrar/junrar: Plain Java unrar library

但是经过调研,发现压缩文件为 rar 还是很困难,没找到便捷可用的方法,这里仅记录 rar 文件解压的坑

 1. Maven 依赖

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

2. 代码实现

import com.github.junrar.Junrar;
import com.github.junrar.exception.RarException;

import java.io.File;
import java.io.IOException;

public class CompressTest {

    public static void main(String[] args) {
        rar();
    }

    private static void rar() {
        try {
            File rar = new File("D:/test/diff.rar");
            File destinationFolder = new File("D:/test/rar");
            Junrar.extract(rar, destinationFolder);
        } catch (IOException e) {
            throw new RuntimeException(e);
        } catch (RarException e) {
            throw new RuntimeException(e);
        }
    }

}

3. 测试与问题

我使用 winrar 创建了一个压缩文件 diff.rar,然后运行代码,抛出以下错误:

17:16:52.273 [main] WARN com.github.junrar.Archive - Support for rar version 5 is not yet implemented!
17:16:52.276 [main] WARN com.github.junrar.Archive - exception in archive constructor maybe file is encrypted, corrupt or support not yet implemented
com.github.junrar.exception.UnsupportedRarV5Exception: null
	at com.github.junrar.Archive.readHeaders(Archive.java:354)
	at com.github.junrar.Archive.setChannel(Archive.java:199)
	at com.github.junrar.Archive.setVolume(Archive.java:811)
	at com.github.junrar.Archive.<init>(Archive.java:149)
	at com.github.junrar.Archive.<init>(Archive.java:170)
	at com.github.junrar.Junrar.createArchiveOrThrowException(Junrar.java:121)
	at com.github.junrar.Junrar.extract(Junrar.java:40)
	at com.github.junrar.Junrar.extract(Junrar.java:32)
	at com.example.demo.test.CompressTest.rar(CompressTest.java:19)
	at com.example.demo.test.CompressTest.main(CompressTest.java:12)
17:16:52.276 [main] ERROR com.github.junrar.Junrar - Error while creating archive
com.github.junrar.exception.UnsupportedRarV5Exception: null
	at com.github.junrar.Archive.readHeaders(Archive.java:354)
	at com.github.junrar.Archive.setChannel(Archive.java:199)
	at com.github.junrar.Archive.setVolume(Archive.java:811)
	at com.github.junrar.Archive.<init>(Archive.java:149)
	at com.github.junrar.Archive.<init>(Archive.java:170)
	at com.github.junrar.Junrar.createArchiveOrThrowException(Junrar.java:121)
	at com.github.junrar.Junrar.extract(Junrar.java:40)
	at com.github.junrar.Junrar.extract(Junrar.java:32)
	at com.example.demo.test.CompressTest.rar(CompressTest.java:19)
	at com.example.demo.test.CompressTest.main(CompressTest.java:12)
Exception in thread "main" java.lang.RuntimeException: com.github.junrar.exception.UnsupportedRarV5Exception
	at com.example.demo.test.CompressTest.rar(CompressTest.java:23)
	at com.example.demo.test.CompressTest.main(CompressTest.java:12)
Caused by: com.github.junrar.exception.UnsupportedRarV5Exception
	at com.github.junrar.Archive.readHeaders(Archive.java:354)
	at com.github.junrar.Archive.setChannel(Archive.java:199)
	at com.github.junrar.Archive.setVolume(Archive.java:811)
	at com.github.junrar.Archive.<init>(Archive.java:149)
	at com.github.junrar.Archive.<init>(Archive.java:170)
	at com.github.junrar.Junrar.createArchiveOrThrowException(Junrar.java:121)
	at com.github.junrar.Junrar.extract(Junrar.java:40)
	at com.github.junrar.Junrar.extract(Junrar.java:32)
	at com.example.demo.test.CompressTest.rar(CompressTest.java:19)
	... 1 more

Process finished with exit code 1

原因是 junrar 不支持 rar5,只要压缩时选择rar4就没问题

重新压缩文件为 rar4

运行代码,发现能正常解压文件了

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java自带的解压缩库不支持RAR文件解压缩,因此需要使用第三方库来实现。其中,比较常用的有两个库:junrar和SevenZipJBinding。 使用junrar库来解压缩RAR文件的示例代码如下: 1. 添加依赖 ``` <dependency> <groupId>com.github.junrar</groupId> <artifactId>junrar</artifactId> <version>1.0.0</version> </dependency> ``` 2. 编写解压缩代码 ``` import com.github.junrar.Archive; import com.github.junrar.rarfile.FileHeader; import java.io.File; import java.io.FileOutputStream; public class RarExtractor { public static void extract(String archivePath, String destinationPath) throws Exception { Archive archive = new Archive(new File(archivePath)); FileHeader fileHeader = archive.nextFileHeader(); while (fileHeader != null) { String fileName = fileHeader.getFileNameW().isEmpty() ? fileHeader.getFileNameString() : fileHeader.getFileNameW(); String filePath = destinationPath + File.separator + fileName; if (fileHeader.isDirectory()) { new File(filePath).mkdirs(); } else { FileOutputStream fos = new FileOutputStream(new File(filePath)); archive.extractFile(fileHeader, fos); fos.close(); } fileHeader = archive.nextFileHeader(); } archive.close(); } public static void main(String[] args) throws Exception { String archivePath = "/path/to/archive.rar"; String destinationPath = "/path/to/destination"; extract(archivePath, destinationPath); } } ``` 使用SevenZipJBinding库来解压缩RAR文件的示例代码如下: 1. 添加依赖 ``` <dependency> <groupId>net.sf.sevenzipjbinding</groupId> <artifactId>sevenzipjbinding</artifactId> <version>9.20-2.00</version> </dependency> ``` 2. 编写解压缩代码 ``` import net.sf.sevenzipjbinding.*; import net.sf.sevenzipjbinding.impl.RandomAccessFileInStream; import net.sf.sevenzipjbinding.simple.ISimpleInArchive; import net.sf.sevenzipjbinding.simple.ISimpleInArchiveItem; import java.io.File; import java.io.FileOutputStream; public class RarExtractor { public static void extract(String archivePath, String destinationPath) throws Exception { RandomAccessFileInStream inputStream = new RandomAccessFileInStream(new File(archivePath)); IInArchive archive = SevenZip.openInArchive(null, inputStream); ISimpleInArchive simpleInArchive = archive.getSimpleInterface(); for (ISimpleInArchiveItem item : simpleInArchive.getArchiveItems()) { String filePath = destinationPath + File.separator + item.getPath(); if (item.isFolder()) { new File(filePath).mkdirs(); } else { FileOutputStream fos = new FileOutputStream(new File(filePath)); byte[] buffer = new byte[4096]; item.extractSlow(new ISequentialOutStream() { @Override public int write(byte[] data) throws SevenZipException { try { fos.write(data); } catch (Exception e) { throw new SevenZipException(e); } return data.length; } }); fos.close(); } } archive.close(); inputStream.close(); } public static void main(String[] args) throws Exception { String archivePath = "/path/to/archive.rar"; String destinationPath = "/path/to/destination"; extract(archivePath, destinationPath); } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值