java解压RAR5

3,540 阅读3分钟

这是我参与更文挑战的第1天,活动详情查看: 更文挑战

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

是 WinRAR 5.0 引入的最新版本的 RAR 格式。它包含很多重要的修改,如 AES-256 加密、更有效的恢复记录、更大的字典大小,较老的软件,包括老版本的 WinRAR,不能解压 RAR 5.0 压缩文件。所以如果你计划把一个压缩文件发送给其他人,选择 RAR5 需要考虑兼容性问题。

RAR5加密算法并未公布,所以很多开源工具包都只支持rar4,在解压rar5格式时,会报出不支持rar5格式的错误,比如常用的junara

PLAN A

经过仔细的翻阅Google,找到了这个: sevenzipjbind.sourceforge.net/

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没公布算法,那我们就自己破解,肝出来!

......

开个玩笑

紧接着我换了一种思路,代码不行,工具来凑,找到了这个: 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.gz
tar -zxvf rarlinux-x64-6.0.2b1.tar.gz
cd rar
make & 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的作用是,等待解压完毕,会去读取目录下的文件,如果不等它的话,就读不到你想要的文件了。

以上就是本次全部内容了,感谢阅读。

更多精彩内容欢迎点击我的博客