This is the first day of my participation in Gwen Challenge

The following is from Winrar. CHM “RAR5”

RAR is the latest version of the RAR format introduced by WinRAR 5.0. It contains many important modifications, such as AES-256 encryption, more efficient recovery of records, larger dictionary sizes, and older software, including older versions of WinRAR, which cannot unzip RAR 5.0 compressed files. So if you plan to send a compressed file to others, there are compatibility issues to choose RAR5.

The RAR5 encryption algorithm has not been published, so many open source toolkits only support RAR4. When unpacking RAR5, errors such as the popular junara will be reported that RAR5 is not supported

PLAN A

Carefully through Google and found this: 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:

In short, 7-zip-jbinding is a c++ version of 7-zip that works just as well as if you had installed 7-zip locally, interacting with jni.

The website has more details and some simple examples:


<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;
}
Copy the code

After testing, this way can achieve decompression of RAR5, but there are still some problems, because of the file encoding problem, there may be garbled files decompressed. There is no parameter in the API to specify the file encoding

PLAN B

Since RAR5 did not publish the algorithm, we will solve it ourselves, liver out!

.

A joke

Then I changed a way of thinking, the code is not good, tools to gather together, found this: www.rarlab.com

Welcome to RARLAB, home of WinRAR and RAR archivers

This description is very comfortable

Support for Windows, Linux, MAC (I used this command on MAC, then found several decompression software have to pay, just brew install rar)

We can call the system script in the code to achieve the purpose of decompressing RAR

Let’s set it up

uname -a 
Select the packet according to the system bitsWget HTTP: / / https://www.rarlab.com/rar/rarlinux-x64-6.0.2b1.tar.gzWget # https://www.rarlab.com/rar/rarlinux-6.0.2b1.tar.gzThe tar - ZXVF rarlinux - x64-6.0.2 b1. Tar. Gzcd rar
make & make install
Copy the code

If you do not have permission, you can ask the operation and maintenance students for help

public class UnrarUtils {
    private static final Logger LOG = LoggerFactory.getLogger(UnrarUtils.class);
    private static final String UNRAR_CMD = "unrar x ";
    * rarFileName RAR file to be decompressed (the path and suffix must be included) * destDir Directory for storing decompressed files */
    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("Decompression completed.");
                returnname; }}catch (Exception e) {
            LOG.warn("Failed to extract RAR file :{}", JSONObject.toJSONString(e));
        }
        returnname; }}Copy the code

Note: process.waitfor () blocks the main thread while a new child thread is opened to perform the task, which can cause other problems if the task is time-consuming.

Of course, in this case, waitFor waits for the decompression to complete, and then reads the files in the directory. If you waitFor waitFor, you won’t be able to read the files you want.

That’s all for this time, thanks for reading.

For more exciting content, please click on my blog