Extract RAR in Java

RAR files are commonly used for compressing large amounts of data into a single, smaller file. However, extracting these files programmatically in Java can be a challenge without the right tools. In this guide, we will walk through two methods for extracting RAR files using Java: one for regular RAR files and another for encrypted RAR files.

Extract RAR Files - Java API Configuration

To unrar files in Java, you need to to install Conholdate.Total for Java while downloading the JAR file or run the following Maven command in the pom.xml file of your project:

<dependency>
<groupId>com.conholdate</groupId>
<artifactId>conholdate-total</artifactId>
<version>24.8</version>
<type>pom</type>
</dependency>

Extract RAR Files in Java

When extracting a regular RAR file, the process involves opening the archive, accessing its individual entries, and saving the extracted data to the desired location. The step-by-step breakdown below demonstrates how to extract the first file entry from a RAR archive and save it to a file. This method is useful for handling standard RAR archives.

  • Load the source RAR archive using the RarArchive class.

  • Extract an archive from the source file.

  • Export the extracted archive to the disk.

The following code snippet shows how to handle the file reading and writing using streams, ensuring that each entry is properly extracted in small chunks to avoid memory overloads, especially when working with large files.

Moreover, you can further enhance this code snippet to iterate through each entry and export it to respective file one-by-one, as per your requirements.

Extract Encrypted RAR Files in Java

When dealing with password-protected or encrypted RAR files, the extraction process is slightly different. You need to provide the correct password to successfully extract the files. The following steps summarise the process to extract encrypted and password-protected RAR files in Java:

  • The input encrypted RAR file is loaded with the RarArchive class.

  • The correct password is provided to extract the first file entry.

  • The extracted data is saved with the specified file name.

The following code sample demonstrates to extract encrypted and password-protected RAR using Java:

Free Evaluation License

You can request a free temporary license to evaluate this and other data compression and marchive manipulation features without any evaluation limitations.

Summing Up

Extracting RAR files in Java, whether standard or encrypted, is a ssimple task with this approach. It offers efficient and flexible ways to handle various RAR archives, including password-protected ones. You can easily integrate RAR extraction functionality into your Java applications, enabling you to manage compressed data seamlessly. If you have any questions, please write to us at forum.

See Also