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

相关推荐
devmoon3 小时前
在 Polkadot Runtime 中添加多个 Pallet 实例实战指南
java·开发语言·数据库·web3·区块链·波卡
Evand J3 小时前
TDOA(到达时间差)的GDOP和CRLB计算的MATLAB例程,论文复现,附参考文献。GDOP:几何精度因子&CRLB:克拉美罗下界
开发语言·matlab·tdoa·crlb·gdop
野犬寒鸦3 小时前
从零起步学习并发编程 || 第七章:ThreadLocal深层解析及常见问题解决方案
java·服务器·开发语言·jvm·后端·学习
云姜.3 小时前
java抽象类和接口
java·开发语言
带刺的坐椅3 小时前
Claude Code Skills,Google A2A Skills,Solon AI Skills 有什么区别?
java·ai·solon·a2a·claudecode·skills
xyq20243 小时前
Pandas 安装指南
开发语言
爱学英语的程序员3 小时前
面试官:你了解过哪些数据库?
java·数据库·spring boot·sql·mysql·mybatis
xixixin_3 小时前
【JavaScript 】从 || 到??:JavaScript 空值处理的最佳实践升级
开发语言·javascript·ecmascript
m0_736919104 小时前
C++中的委托构造函数
开发语言·c++·算法
lsx2024064 小时前
Python3 SMTP发送邮件教程
开发语言