How can unrar a file with python
Asked Answered
S

5

48

How can I extract a .zip or .rar file using Python?

Storms answered 12/7, 2013 at 12:0 Comment(0)
M
50

Late, but I wasn't satisfied with any of the answers.

pip install patool
import patoolib
patoolib.extract_archive("foo_bar.rar", outdir="path here")

Works on Windows and linux without any other libraries needed.

Matney answered 3/10, 2014 at 11:45 Comment(2)
Good library, but very few flexibility. For example, it cannot overwrite GZ files. See this question for more details: #29632293Scope
I tried the above and got patoolib.util.PatoolError: could not find an executable program to extract format rar; candidates are (rar,unrar,7z), If I am understanding it correctly, I need to have one of the utilities, which defeats the purpose.Ogata
H
24

Try the pyunpack package:

from pyunpack import Archive
Archive('a.zip').extractall('/path/to')
Herzegovina answered 12/7, 2013 at 12:16 Comment(2)
does it require patool or some othrs ?Seibold
yes without it only zip files can be extracted. look in the pyunpack documentation the link I provided there is everythingHerzegovina
F
13

A good package for it is rarfile :

Here is an example:

import rarfile

rf = rarfile.RarFile("myarchive.rar")
for f in rf.infolist():
    print(f.filename, f.file_size)
    if f.filename == "README":
        print(rf.read(f))

Infos and docs here :

https://pypi.python.org/pypi/rarfile/

https://rarfile.readthedocs.io/api.html

Frambesia answered 12/7, 2013 at 12:10 Comment(5)
If you decided to use rarfile you might have a problem when trying to extract a file. This is because the extraction is using the UnRaR.exe tool from winrar website (rarlab.com/rar_add.htm). Direct link for windows: rarlab.com/rar/unrarw32.exe. Make sure to have this file. I put it in C:\Python27\UnRar.exe. Edit the file: C:\Python27\Lib\site-packages\rarfile.py like that: UNRAR_TOOL = r"c:\python27\unrar.exe" It helped me.Ash
No, it needs unrar.exe which is not available on LinuxPareto
@MaksymGanenko: On Linux it needs the unrar executable, which can be installed using the package manager. On Debian/Ubuntu it's sudo apt install unrarBrierwood
for listing archive member names it doesn't require unrar, however, to extract and update it is required. You can have the open source program unar instead of unrar as a backed as well.Anabel
For windows it worked perfect for me, but the other packages introduced in the other answer didn't.Motorbus
O
13

After some deep diving, here are my findings:

  • RAR is not an free open format and is owned by RARLabs. You must install their DLL or exe first to work with RAR. Some programs like 7zip might already include this with them.
  • patool is application that provides uniform command line as wrapper to other external compression applications. Natively, it can only deal with TAR, ZIP, BZIP2 and GZIP without needing external support.
  • pyunpack is Python library that can only deal with zip natively but provides interface to patool.

With this in mind, following things worked for me:

  • Make sure 7zip is installed
  • pip install patool pyunpack

Then to use it,

import pyunpack

pyunpack.Archive(archive_file).extractall(extract_dir)
Overcapitalize answered 27/6, 2020 at 23:42 Comment(0)
U
4

This method only requires having 7zip installed.
Works flawlessly on every system 7zip does.
No python packages needed at all.

import subprocess
subprocess.run('7z x -oOutdir archive.rar')

The subprocess module is comes with python.

Unlikelihood answered 14/2, 2022 at 1:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.