rarfile
| Language | Python 3.10+ |
|---|---|
| License | ISC |
| Repository | github.com/markokr/rarfile |
| Documentation | rarfile.readthedocs.io |
| Depends on | an extraction tool: unrar (recommended), 7z / 7zz, unar or bsdtar; cryptography or pycryptodome for archives with encrypted headers |
| Read / write | yes / no; RAR3/RAR4 and RAR5, passwords, multi-volume, comments |
rarfile parses RAR headers in Python and gives you a RarFile object that behaves like zipfile.ZipFile. Stored (uncompressed) entries are read directly. Compressed entries are decompressed by running an external tool and reading its output through a pipe, so the speed and supported features depend on the tool:
| Tool | Notes |
|---|---|
unrar |
full support; freeware from RARLAB (non-free package on Linux) |
7z / 7zz |
full support; on Debian / Ubuntu needs the 7zip-rar package |
unar |
no RAR5 BLAKE2 checks; doesn’t work on Windows |
bsdtar |
no volumes, no solid archives, no passwords |
rarfile picks the first tool it finds on PATH.
Installation#
pip install rarfile
# plus a backend
sudo apt install unrar # Debian / Ubuntu (non-free / multiverse)
sudo apt install 7zip 7zip-rar # or 7-Zip
brew install sevenzip # macOS: provides 7zz
On Windows, put UnRAR.exe (from rarlab.com) or 7-Zip’s 7z.exe in a folder on PATH.
Basic usage: list and extract a RAR file#
import rarfile
with rarfile.RarFile("archive.rar") as rf:
for info in rf.infolist():
kind = "dir " if info.is_dir() else "file"
print(kind, info.filename, info.file_size, info.mtime)
rf.extractall("output")
extractall keeps the folder structure; rf.extract("docs/readme.txt", "output") extracts one member.
How to read a file from a RAR without extracting to disk#
import rarfile
with rarfile.RarFile("archive.rar") as rf:
data = rf.read("docs/readme.txt") # bytes
with rf.open("data/log.csv") as f: # file-like object, streams
for line in f:
print(line.decode("utf-8").rstrip())
How to extract a password-protected RAR#
import rarfile
try:
with rarfile.RarFile("secret.rar") as rf:
if rf.needs_password():
rf.setpassword("password")
rf.extractall("output")
except rarfile.PasswordRequired:
print("password required")
except rarfile.RarWrongPassword:
print("wrong password")
You can also pass pwd="..." to open, read, extract and extractall. For archives with encrypted file names, call setpassword before reading the file list (rarfile then needs cryptography or pycryptodome).
How to check if a file is a RAR archive#
import rarfile
if rarfile.is_rarfile("download.bin"):
with rarfile.RarFile("download.bin") as rf:
print("solid" if rf.is_solid() else "not solid", len(rf.namelist()), "files")
is_rarfile checks the signature; is_rarfile_sfx also finds RAR data inside self-extracting .exe files.
How to extract a multi-volume RAR#
Open the first volume; rarfile follows .part2.rar or .r00 names on its own:
import rarfile
with rarfile.RarFile("movie.part1.rar") as rf:
print(rf.volumelist()) # all volume files
rf.extractall("output")
Opening a later volume raises rarfile.NeedFirstVolume.
Tips#
rf.testrar()checks every entry’s CRC and raises on damage.- Catch
rarfile.RarCannotExecto tell users to installunraror 7-Zip. rf.commentandinfo.commenthold archive and file comments.