zip files support in Python
CLI
The
zipfile module provides a simple command-line interface to interact with ZIP archives.
Create zip archive from files:
python3 -m zipfile -c etc.zip /etc/hosts /etc/issue /etc/passwd
Create zip archive from directories:
python3 -m zipfile -c yum.zip /etc/yum
If you want to extract a zip archive into the specified directory, use the
-e option:
python3 -m zipfile -e yum.zip yum-dir/
For a list of the files in a zip archive, use the
-l option:
python3 -m zipfile -l etc.zip
Test whether the zipfile is valid or not:
python3 -m zipfile -t yum.zip
Python code
from zipfile import ZipFile
with ZipFile('yum.zip', 'r') as zip_obj:
zip_obj.extractall() # extract in current directory
# zip_obj.extractall(path="./yumfolder") # extract in ./yumfolder" directory
# list of the files in a zip archive
print(zip_obj.namelist())
Documentation
https://docs.python.org/3/library/zipfile.html