以下内容来自《WinRAR.chm》"RAR5"

是 WinRAR 5.0 引入的最新版本的 RAR 格局。它蕴含很多重要的批改,如 AES-256 加密、更无效的复原记录、更大的字典大小,较老的软件,包含老版本的 WinRAR,不能解压 RAR 5.0 压缩文件。所以如果你打算把一个压缩文件发送给其他人,抉择 RAR5 须要思考兼容性问题。

RAR5加密算法并未颁布,所以很多开源工具包都只反对rar4,在解压rar5格局时,会报出不反对rar5格局的谬误,比方罕用的junara

PLAN A

通过认真的翻阅Google,找到了这个: http://sevenzipjbind.sourcefo...

7-Zip-JBinding is a java wrapper for 7-Zip C++ library. It allows extraction of many archive formats using a very fast native library directly from java through JNI. Features:

简而言之,7-Zip-JBinding 是一个c++版7-Zip的封装,就和在你本地装置了7-Zip是相似的成果,通过jni交互。

官网有更具体的介绍,和一些简略的例子:

<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>private int getNumberOfItemsInArchive(String archiveFile) throws Exception {    IInArchive archive;    RandomAccessFile randomAccessFile;    randomAccessFile = new RandomAccessFile(archiveFile, "r");    archive = SevenZip.openInArchive(ArchiveFormat.ZIP, // null - autodetect            new RandomAccessFileInStream(randomAccessFile));    int numberOfItems = archive.getNumberOfItems();    archive.close();    randomAccessFile.close();    return numberOfItems;}

通过实测,这种形式是能够实现解压rar5的,然而还有一些问题,因为文件编码问题,可能会呈现解压出的文件存在乱码的状况。这种状况临时不晓得怎么解决,API上没有相干参数能够指定文件编码

PLAN B

既然RAR5没颁布算法,那咱们就本人破解,肝进去!

......

开个玩笑

紧接着我换了一种思路,代码不行,工具来凑,找到了这个: http://www.rarlab.com

Welcome to RARLAB, home of WinRAR and RAR archivers

这个形容就很难受

反对windows、linux、mac(我在mac用的就是这个命令,过后找了好几个解压rar的软件都要付费,索性brew install rar)

咱们能够在代码里调用零碎脚本,来达到解压rar的目标

先来装置一下

uname -a # 依据零碎位数抉择对应的包wget https://www.rarlab.com/rar/rarlinux-x64-6.0.2b1.tar.gz# wget https://www.rarlab.com/rar/rarlinux-6.0.2b1.tar.gztar -zxvf rarlinux-x64-6.0.2b1.tar.gzcd rarmake & make install

如果你没有权限的话,能够找运维同学帮忙

public class UnrarUtils {    private static final Logger LOG = LoggerFactory.getLogger(UnrarUtils.class);    private static final String UNRAR_CMD = "unrar x ";    /**     * 将1个RAR文件解压     * rarFileName 须要解压的RAR文件(必须蕴含门路信息以及后缀)     * destDir 解压后的文件搁置目录     */    public static String unRARFile(String filepath) {        String name = filepath.substring(0, filepath.lastIndexOf('.'));        File targetDir = new File(name);        if (!targetDir.exists()) {            targetDir.mkdirs();        }        String cmd = UNRAR_CMD + filepath + " " + name;        try {            Runtime rt = Runtime.getRuntime();            Process process = rt.exec(cmd);            int retCode = process.waitFor();            if (retCode == 0) {                LOG.info("解压结束");                return name;            }        } catch (Exception e) {            LOG.warn("解压rar文件失败:{}", JSONObject.toJSONString(e));        }        return name;    }}

留神:process.waitFor() 会阻塞主线程,同时新开一个子线程去执行工作,如果工作耗时的话, 可能会引起一些其余的问题。

当然本例中,waitFor的作用是,期待解压结束,会去读取目录下的文件,如果不等它的话,就读不到你想要的文件了。

以上就是本次全部内容了,感激浏览。