关于java:解压RAR5

5次阅读

共计 2513 个字符,预计需要花费 7 分钟才能阅读完成。

以下内容来自《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.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 的作用是,期待解压结束,会去读取目录下的文件,如果不等它的话,就读不到你想要的文件了。

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

正文完
 0