找不到rar.RarArchiveInputStream?JAVA解压RAR5的方案。

先说结论:不要再被网络ai生文给误导了,RarArchiveInputStream包根本就不存在!!

最近做一个JAVA解压rar5的接口,被网上的文章误导了,原文:

引入RarArchiveInputStream_mob64ca12f0cf8f的技术博客_51CTO博客

在此过程中,我发现org.apache.commons.compress.archivers根本导不进来rar,以为是依赖的版本问题,接连换了好几个版本,都没找到rar,于是我去官网看了一眼(下图),发现里面根本就没有rar的包,索性直接放弃了这种方案。

还有一种方案是导入junrar,但junrar包不能满足RAR5的解压。

最后我找到了这篇文章:
Java解压RAR5 - fan93 - 博客园

防止链接失效,我把关键内容贴出来,再次感谢这篇文章的博主!

1.添加pom文件依赖
XML 复制代码
        <dependency>
            <groupId>com.github.axet</groupId>
            <artifactId>java-unrar</artifactId>
            <version>1.7.0-8</version>
        </dependency>
        <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>
2.例子
java 复制代码
package rar5;
 
import java.io.IOException;
import java.io.RandomAccessFile;
 
import net.sf.sevenzipjbinding.IInArchive;
import net.sf.sevenzipjbinding.SevenZip;
import net.sf.sevenzipjbinding.impl.RandomAccessFileInStream;
 
public class RAR5Test {
	public static void main(String[] args) throws IOException {
	    String rarDir = "D:\\rar5.rar";
	    String outDir = "D:\\rar5";
	    IInArchive archive;
            RandomAccessFile randomAccessFile;
            // 第一个参数是需要解压的压缩包路径,第二个参数参考JdkAPI文档的RandomAccessFile
            //r代表以只读的方式打开文本,也就意味着不能用write来操作文件
            randomAccessFile = new RandomAccessFile(rarDir, "r");
            archive = SevenZip.openInArchive(null, // null - autodetect
                    new RandomAccessFileInStream(randomAccessFile));
            int[] in = new int[archive.getNumberOfItems()];
            for (int i = 0; i < in.length; i++) {
                in[i] = i;
            }
            archive.extract(in, false, new ExtractCallback(archive,unRarDir.getAbsolutePath() + "/"));
            archive.close();
            randomAccessFile.close();
 
	}
}
3.编写回调ExtractCallback
java 复制代码
public class ExtractCallback implements IArchiveExtractCallback {

    private int index;
    private IInArchive inArchive;
    private String ourDir;

    public ExtractCallback(IInArchive inArchive, String ourDir) {
        this.inArchive = inArchive;
        this.ourDir = ourDir;
    }

    @Override
    public void setCompleted(long arg0) throws SevenZipException {
    }

    @Override
    public void setTotal(long arg0) throws SevenZipException {
    }

    @Override
    public ISequentialOutStream getStream(int index, ExtractAskMode extractAskMode) throws SevenZipException {
        this.index = index;
        final String path = (String) inArchive.getProperty(index, PropID.PATH);
        final boolean isFolder = (boolean) inArchive.getProperty(index, PropID.IS_FOLDER);
        return data -> {
            try {
                if (!isFolder) {
                    File file = new File(ourDir + path);
                    save2File(file, data);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            return data.length;
        };
    }

    @Override
    public void prepareOperation(ExtractAskMode arg0) throws SevenZipException {
    }

    @Override
    public void setOperationResult(ExtractOperationResult extractOperationResult) throws SevenZipException {

    }

    public static boolean save2File(File file, byte[] msg) {
        OutputStream fos = null;
        try {
            File parent = file.getParentFile();
            if ((!parent.exists()) && (!parent.mkdirs())) {
                return false;
            }
            fos = new FileOutputStream(file, true);
            fos.write(msg);
            fos.flush();
            return true;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return false;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        } finally {
            try {
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

Chinese-English Dictionary
Enable Select Search
My Words

相关推荐
JienDa2 小时前
PHP 静态分析工具实战:PHPStan 和 Psalm 完全指南
开发语言·php
jiayong232 小时前
AI应用领域编程语言选择指南:Java vs Python vs Go
java·人工智能·python
XXYBMOOO2 小时前
Qt 调用 DLL 实现固件升级进度弹窗(完整实战案例)
开发语言·qt·性能优化·简单工厂模式
胖咕噜的稞达鸭2 小时前
【C语言进阶】死磕指针:从内存原理到指针数组的深度解析
c语言·开发语言·网络
lly2024062 小时前
Pandas 相关性分析
开发语言
bjzhang752 小时前
IDEA 2025.3重磅发布,Ultimate 终极版和 Community社区版二合一,免费版可商用
java·idea
CHINAHEAO2 小时前
Bagisto修复php弃用警告,看着难受
开发语言·php
程序猿零零漆2 小时前
Spring之旅 - 记录学习 Spring 框架的过程和经验(三)Bean的依赖注入配置、Spring的其它配置标签
java·学习·spring
博大世界2 小时前
Python打包成exe文件方法
开发语言·python