How can I extract a .zip
or .rar
file using Python?
How can unrar a file with python
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.
Good library, but very few flexibility. For example, it cannot overwrite GZ files. See this question for more details: #29632293 –
Scope
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 Try the pyunpack
package:
from pyunpack import Archive
Archive('a.zip').extractall('/path/to')
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 everything –
Herzegovina
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 :
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 Linux –
Pareto @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
–
Brierwood 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
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 topatool
.
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)
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.
© 2022 - 2024 — McMap. All rights reserved.