找不到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

相关推荐
程序员清风12 小时前
程序员兼职必看:靠谱软件外包平台挑选指南与避坑清单!
java·后端·面试
皮皮林55113 小时前
利用闲置 Mac 从零部署 OpenClaw 教程 !
java
华仔啊19 小时前
挖到了 1 个 Java 小特性:var,用完就回不去了
java·后端
SimonKing19 小时前
SpringBoot整合秘笈:让Mybatis用上Calcite,实现统一SQL查询
java·后端·程序员
日月云棠1 天前
各版本JDK对比:JDK 25 特性详解
java
用户8307196840821 天前
Spring Boot 项目中日期处理的最佳实践
java·spring boot
JavaGuide2 天前
Claude Opus 4.6 真的用不起了!我换成了国产 M2.5,实测真香!!
java·spring·ai·claude code
IT探险家2 天前
Java 基本数据类型:8 种原始类型 + 数组 + 6 个新手必踩的坑
java
花花无缺2 天前
搞懂new 关键字(构造函数)和 .builder() 模式(建造者模式)创建对象
java
用户908324602732 天前
Spring Boot + MyBatis-Plus 多租户实战:从数据隔离到权限控制的完整方案
java·后端