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

相关推荐
shjita几秒前
记录java执行中的一个错误细节
java·开发语言
空中海1 分钟前
Docker入门到精通
java·docker·eureka
AI进化营-智能译站7 分钟前
ROS2 C++开发系列14-Lambda表达式处理传感器数据流|文件IO保存机器人实验日志
开发语言·c++·ai·机器人
itzixiao13 分钟前
L1-067 洛希极限(10分)[java][python]
java·开发语言·算法
java1234_小锋21 分钟前
Spring AI 2.0 开发Java Agent智能体 - Spring AI项目调用本地Ollama模型
java·人工智能·spring·spring ai2.0
二哈赛车手21 分钟前
新人笔记---多策略搭建策略执行链实现RAG检索后过滤
java·笔记·spring·设计模式·ai·策略模式
PESS ABIN22 分钟前
JavaWeb项目打包、部署至Tomcat并启动的全程指南(图文详解)
java·tomcat
l1t24 分钟前
DeepSeek总结的DuckDB anofox-forecast季节调整时间序列预测插件功能
开发语言·数据库
xyq202425 分钟前
SVG 阴影
开发语言
好奇龙猫27 分钟前
[大学院ーpython-base learning3: python and recommendation system ]
开发语言·python