50

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

5 Answers 5

53

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.

2
  • Good library, but very few flexibility. For example, it cannot overwrite GZ files. See this question for more details: stackoverflow.com/questions/29631793/… Commented Apr 27, 2015 at 11:00
  • 67
    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.
    – some user
    Commented Dec 11, 2016 at 7:34
24

Try the pyunpack package:

from pyunpack import Archive
Archive('a.zip').extractall('/path/to')
2
  • 2
    does it require patool or some othrs ?
    – Basj
    Commented Jan 31, 2014 at 12:19
  • 1
    yes without it only zip files can be extracted. look in the pyunpack documentation the link I provided there is everything Commented Feb 7, 2014 at 10:30
14

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)
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

5
  • 1
    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.
    – E235
    Commented Apr 4, 2017 at 13:25
  • 1
    No, it needs unrar.exe which is not available on Linux Commented May 8, 2020 at 17:40
  • 1
    @MaksymGanenko: On Linux it needs the unrar executable, which can be installed using the package manager. On Debian/Ubuntu it's sudo apt install unrar
    – MestreLion
    Commented Aug 7, 2020 at 7:27
  • 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. Commented Oct 26, 2020 at 20:48
  • For windows it worked perfect for me, but the other packages introduced in the other answer didn't. Commented Apr 28, 2021 at 9:23
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.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.