Zip
With zip you can package multiple files in a single archive file.
"""Zip arhive
Compressing multiple files into a single archive file.
"""
import zipfile, os
DIR = os.path.dirname(os.path.realpath(__file__))
newZip = zipfile.ZipFile(DIR + '/data/archive.zip', 'w')
newZip.write(DIR + '/data/file1.txt', 'file1')
newZip.write(DIR + '/data/file2.txt', 'file2')
newZip.close()
archive = zipfile.ZipFile(DIR + '/data/archive.zip')
print(archive.namelist())
# file1, file2
info = archive.getinfo('file1')
print(info.file_size)
# 3
archive.close()
Extract
Extract all files into the working directory.
"""Extract from zip file.
Extract all files into the current directory.
"""
import zipfile, os
from pathlib import Path
DIR = Path(__file__).resolve().parent
with zipfile.ZipFile(DIR / 'data/archive.zip') as archive:
archive.extractall(DIR / 'data/extracted')
for root, dirs, files in os.walk(DIR / 'data/extracted'):
for name in files:
print(name)
# file1
# file2
Last update: 308 days ago