Master Python Zipfile: Read, Extract, and Create ZIP Archives Efficiently
Learn how to use Python's zipfile module to inspect ZIP archives, extract their contents, and create new compressed files, with clear code examples demonstrating reading file info, extracting all files, and adding files in write or append mode.
Most people are familiar with the .zip format, which bundles multiple files into a single archive for easier transmission and storage.
This article shows how to work with ZIP files in Python using the built‑in zipfile module.
1. Read ZIP File Information
To inspect a ZIP archive you first create a ZipFile object, similar to a regular file object.
import zipfile
zip_file = zipfile.ZipFile('zfile.zip')
# List all entries in the archive
f_content = zip_file.namelist()
# Size before compression
f_size = zip_file.getinfo('zfile/a.txt').file_size
# Size after compression
c_size = zip_file.getinfo('zfile/a.txt').compress_sizeThe namelist() method returns a list of all file and folder names in the archive. These names can be passed to getinfo() to obtain a ZipInfo object, which provides attributes such as file_size (original size) and compress_size (compressed size).
2. Extract Files from a ZIP Archive
The extractall() method extracts every file and folder in the archive to the current working directory (or to a directory you specify).
import zipfile
zip_file = zipfile.ZipFile('zfile.zip')
zip_file.extractall()
zip_file.close()You can pass a target folder to extractall(); if the folder does not exist, it will be created automatically.
3. Create and Add to a ZIP Archive
To create a new ZIP file you open a ZipFile object in write mode by passing 'w' as the second argument.
import zipfile
zip_file = zipfile.ZipFile('new.zip', 'w')
# Add an entire directory
zip_file.write('zfile', compress_type=zipfile.ZIP_DEFLATED)
# Add a single file (example)
# zip_file.write('c.txt', compress_type=zipfile.ZIP_DEFLATED)
zip_file.close()Opening a ZIP file in write mode overwrites any existing content. To add files to an existing archive, open it in append mode by using 'a' instead of 'w'.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
