Registry / serialization / rarfile

rarfile

JSON →
library4.5pypypi✓ verified 25d ago

rarfile is a Python library for reading RAR archives, providing a `zipfile`-like interface. It acts as a wrapper around external utilities like `unrar`, `7zip`, `unar`, or `unrar-free`. The project is actively maintained with regular updates and bug fixes.

pip install rarfile
INSTALL
IMPORT
SIG · RARFILE
R
rarfile
serializationpythonv4.5
Install
1.5s avg
Import
44ms
Disk
16MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v4.5 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.046s · 18MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 1.5s · import 0.042s · 18MB
16MB installed
● package 16MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

RarFile
from rarfile import RarFile

This quickstart demonstrates how to open a RAR archive, list its contents, and shows commented-out examples for extracting files. It requires an external `unrar` binary (or similar) to be installed on your system and accessible via PATH for `rarfile` to function.

import rarfile import os # NOTE: This example requires an external 'unrar' (or 7zip/unar) binary # to be installed and available in your system's PATH. # Create a dummy rar file for demonstration (requires external tool) # If you don't have one, replace 'example.rar' with an existing file. # For a real scenario, you'd have an actual .rar file. # Placeholder for a RAR file path rar_file_path = 'example.rar' # If you have an unrar tool, you could try creating a dummy rar file. # For this example, we'll assume one exists or will be created externally. # Example: os.system('rar a example.rar dummy_file.txt') # This is out of scope for the quickstart, assuming the rar exists. if not os.path.exists(rar_file_path): print(f"Warning: '{rar_file_path}' not found. Please create or provide an actual RAR file for this example.") print("Skipping quickstart execution due to missing RAR file.") else: try: with rarfile.RarFile(rar_file_path, 'r') as rf: print(f"Contents of {rar_file_path}:") for info in rf.infolist(): print(f" - {info.filename} ({info.file_size} bytes)") # Example: Extract a specific file (if it exists) # file_to_extract = 'some_file_in_rar.txt' # if file_to_extract in rf.namelist(): # print(f"\nExtracting {file_to_extract}...") # rf.extract(file_to_extract, path='./extracted_files') # print(f"Extracted {file_to_extract} to ./extracted_files") # Example: Extract all files # print("\nExtracting all files...") # rf.extractall(path='./extracted_all') # print("All files extracted to ./extracted_all") except rarfile.RarFileError as e: print(f"Error opening RAR file: {e}") except Exception as e: print(f"An unexpected error occurred: {e}")
Debug
Known issues
gotchararfile itself does not contain RAR decompression logic. It acts as a wrapper and requires an external binary like `unrar` (the official RARLAB utility), `7zip` (`p7zip` on Linux), `unar`, or `unrar-free` to be installed on your system and available in your PATH.
fix
Install one of the supported external RAR utilities (e.g., `sudo apt-get install unrar` on Debian/Ubuntu, `brew install unrar` on macOS, or download from RARLAB for Windows) and ensure it's accessible via your system's PATH.
affects: All versions
breakingStarting with v4.0, directory names in the `infolist()` and `namelist()` will consistently have a '/' appended at the end to match `zipfile` behavior. This might break code that expects directory names without a trailing slash or uses string matching.
fix
Adjust code to expect or strip trailing slashes for directory names. For example, `entry.filename.rstrip('/')` if you need the name without the slash.
affects: 4.0 and later
breakingIn v4.0, the internal `RarFile.extract` implementation was rewritten to use `RarFile.open`. As a result, certain advanced features like hard links, NTFS streams, and junctions are no longer supported by `rarfile`'s internal extraction mechanism.
fix
If these features are critical, consider using the external `unrar` tool directly or downgrading to a version prior to 4.0 (not recommended due to other improvements).
affects: 4.0 and later
gotchaWhile v4.1 improved RAR5 password handling by checking the password before attempting to read file data, ensuring correct password provision for encrypted RAR5 archives is crucial. Incorrect passwords will still lead to errors.
fix
Always provide the correct password using the `pwd` argument when opening encrypted RAR files: `rarfile.RarFile(filename, 'r', pwd=b'your_password')`. Note that the password should be bytes.
affects: All versions, improved in 4.1
gotchaIn v4.2, support for Python 3.7 was dropped from the CI/testing matrix. While it might still function, official support and testing are no longer guaranteed for Python 3.7.
fix
Upgrade your Python environment to 3.8 or newer (3.12 is now supported and tested).
affects: 4.2 and later
Errors
Common errors & fixes
rarfile.MissingExternalError: Missing an external unpacker. Look for unrar, 7z, unar or unrar-free in your PATH.
The `rarfile` library requires an external utility (like `unrar`, `7z`, `unar`, or `unrar-free`) to extract RAR archives, but none of these executables could be found in the system's PATH environment variable.
fix
Install one of the required external utilities (e.g., `unrar` on Linux/macOS, 7-Zip on Windows) and ensure its executable is discoverable in your system's PATH. For example, on Ubuntu: `sudo apt-get install unrar`.
rarfile.BadRarFile: Not a RAR file or a corrupted RAR file.
The provided file is either not a valid RAR archive, is corrupted, or is encrypted with an unsupported compression method or header format that `rarfile` (or its underlying external tool) cannot process.
fix
Ensure the file is a legitimate, uncorrupted RAR archive. If it's encrypted, `rarfile` might require the correct password, or the encryption might be of an unsupported type. Try opening the file with a standalone RAR extractor to confirm its integrity.
rarfile.RarExecError: Failed to extract RAR: The archive is password protected, but no password was given.
The RAR archive is password-protected, but no password was provided to the `rarfile.RarFile` constructor or the `extract()` method, or the provided password was incorrect.
fix
Provide the correct password when opening the RAR file or extracting its contents using the `pwd` parameter: `rf = rarfile.RarFile('archive.rar', pwd='your_password')` or `rf.extractall(pwd='your_password')`.
ModuleNotFoundError: No module named 'rarfile'
The `rarfile` Python package has not been installed in the current Python environment.
fix
Install the package using pip: `pip install rarfile`.
Upgrade
Version history
4.5latest on PyPI · released Aug 2, 2026
Audit
Dependencies

No dependency data recorded yet.

Agent activity
17 hits · last 30 days
node
14
OpenAI (training)
1
Resources
rarfile — pip install rarfile · libregistry