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

相关推荐
冰冰菜的扣jio14 分钟前
Redis缓存问题——一致性问题、事务、持久化
java·spring·mybatis
施棠海24 分钟前
监听与回调的三个demo
java·开发语言
時肆48524 分钟前
C语言造轮子大赛:从零构建核心组件
c语言·开发语言
赴前尘1 小时前
golang 查看指定版本库所依赖库的版本
开发语言·后端·golang
de之梦-御风1 小时前
【C#.Net】C#开发的未来前景
开发语言·c#·.net
毕设源码-钟学长1 小时前
【开题答辩全过程】以 家政服务平台为例,包含答辩的问题和答案
java
知乎的哥廷根数学学派1 小时前
基于数据驱动的自适应正交小波基优化算法(Python)
开发语言·网络·人工智能·pytorch·python·深度学习·算法
de之梦-御风2 小时前
【C#.Net】C#在工业领域的具体应用场景
开发语言·c#·.net
sunfove2 小时前
将 Python 仿真工具部署并嵌入个人博客
开发语言·数据库·python
Learner2 小时前
Python类
开发语言·python