Extract RAR Archives in Python

File compression and archiving are fundamental tasks in the world of computing to save storage space and make data transfer more manageable. A common file compression format is RAR (Roshal Archive), which is known for its high compression ratio and versatility. In this blog post, we will explore how to extract RAR archives in Python using a powerful RAR library that simplifies working with various archive formats.

Python RAR Extractor Library

Aspose.ZIP for Python is a robust and feature-rich library that is developed to automate file compressing and archiving tasks in Python applications. Aspose.ZIP simplifies working with compressed archive files, including popular formats like ZIP, RAR, 7z, and more. Using the library, you can easily extract RAR files in a few lines of code.

You can either download the library or install it from PyPI.

> pip install aspose-zip

Unrar a RAR Archive in Python

Aspose.ZIP for Python has simplified the process of extracting files from RAR archives. Simply load the RAR archive and extract it into the desired folder. Let’s have a look at the steps required to unrar a RAR archive in Python.

  1. First, load the RAR file using RarArchive class.
  2. Then, call RarArchive.extract_to_directory(folder_name) method to extract the files in the archive.

The following code sample shows how to extract a RAR archive in Python.

Unrar a Single File in RAR Archive

Instead of extracting all the entries in a RAR archive, you can opt to extract only the desired files/entries. For this, you can access the entry using its index. The following are the steps to extract a particular file from a RAR archive in Python.

  1. First, load the RAR file using RarArchive class.
  2. Then, extract file using RarArchive.entries[index].extract(file_name) method.

The following code sample shows how to extract a desired file entry from a RAR archive.

Extract an Encrypted RAR Archive in Python

There would be cases when you receive an encrypted RAR archive or an archive having encrypted entries protected by a password. In such cases, you need to provide the password while extracting the RAR files.

Extracting an Encrypted RAR Archive

To unrar an encrypted RAR archive, provide the password as the second parameter of the method RarArchive.extract_to_directory(folder_name, password) method.

The following code snippet demonstrates how to unrar an encrypted RAR archive in Python.

Extract an Encrypted RAR Entry

To extract a single file that is encrypted with a password, you can provide the password as a second parameter of the method RarArchive.entries[index].extract(file_name, password).

The following code sample demonstrates how to extract an encrypted RAR entry in Python.

Get a Free License

You can get a free temporary license and extract RAR archives without any evaluation limitations.

Python RAR Library - Resources

You can explore more about our Python RAR library using the following resources:

Extract RAR Archives Online

You can also use our free online app to extract RAR archives, which is powered by Aspose.ZIP for Python.

Conclusion

In this blog post, we discussed extracting RAR archives in Python in different ways. The step by step guidelines and code samples have demonstrated how to extract RAR archives, unrar a single RAR entry, or extract encrypted RAR archives in Python. By utilizing our powerful RAR extractor library, you can effortlessly extract the contents of RAR files, making data manipulation and analysis more accessible and efficient.

See also