Python ZipFile Package Guide | Generated by AI
Overview of the ZipFile Package
Python’s zipfile
module allows you to read from and write to ZIP archive files without external tools. It’s part of the standard library, so no installation is needed (from Python 2.3+). Use it for compressing, decompressing, or manipulating ZIP files programmatically. It supports various compression methods like DEFLATE.
Import it with: import zipfile
Reading ZIP Files
To extract or inspect existing ZIP files:
- Open a ZIP file for reading:
with zipfile.ZipFile('example.zip', 'r') as zip_ref: # 'r' is for read mode
- List contents:
with zipfile.ZipFile('example.zip', 'r') as zip_ref: file_list = zip_ref.namelist() # Returns list of file names print(file_list)
- Extract files:
- Extract all:
zip_ref.extractall('destination_folder')
- Extract one:
zip_ref.extract('file_inside.zip', 'path')
- Extract all:
- Read file content without extracting:
with zipfile.ZipFile('example.zip', 'r') as zip_ref: with zip_ref.open('file_inside.zip') as file: content = file.read() print(content.decode()) # If it's text
Note: Always use with
for automatic closing. For password-protected ZIPs, add pwd=b'password'
to ZipFile()
.
Writing ZIP Files
To create or add to new/existing ZIP files:
- Create a new ZIP file:
with zipfile.ZipFile('new_archive.zip', 'w') as zip_ref: # 'w' is for write mode (overwrites if exists)
- Add files:
- Add one:
zip_ref.write('source_file.txt', 'arcname.txt')
(optional arcname renames it inside) - Add multiple: Loop over a list of files and call
write()
.
- Add one:
- Compress as you write (default is DEFLATE):
with zipfile.ZipFile('archive.zip', 'w', zipfile.ZIP_DEFLATED) as zip_ref: zip_ref.write('large_file.txt')
Options:
ZIP_STORED
(no compression),ZIP_DEFLATED
,ZIP_BZIP2
, etc. - Append to existing ZIP (use ‘a’ mode instead of ‘w’).
Common Operations and Tips
- Check if a file is a valid ZIP: Use
zipfile.is_zipfile('file.zip')
. - Get file info:
zip_ref.getinfo('file.txt')
returns a ZipInfo object with details like size, date. - Handle large files: Reading/extracting large files might consume memory; use
open()
method for streaming. - Error handling: Catch
BadZipFile
orLargeZipFile
exceptions. - Cross-platform: Works on Windows, macOS, Linux, but be mindful of path separators (use
/
). - Encryption: Basic password protection is supported, but it’s weak—use stronger tools for security.
- Higher-level: For more features (e.g., shutil integration), combine with
shutil.make_archive()
for creating ZIPs.
Example complete script to zip a folder:
import zipfile
import os
def zip_folder(source_dir, zip_path):
with zipfile.ZipFile(zip_path, 'w', zipfile.ZIP_DEFLATED) as zipf:
for root, dirs, files in os.walk(source_dir):
for file in files:
zipf.write(os.path.join(root, file),
os.path.relpath(os.path.join(root, file), os.path.join(source_dir, '..')))
zip_folder('my_folder', 'output.zip')
For full documentation, check the official Python docs. If you have a specific task (e.g., “how to extract to memory”), provide more details!