Install & Compatibility
Where this runs
tested against v0.1.3 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
muslpy 3.10–3.95 runs
installs and imports cleanly · install 0.0s · import 0.000s · 17.9MB
glibcpy 3.10–3.95 runs
installs and imports cleanly · install 1.5s · import 0.000s · 18MB
16MB installed
● package 16MB
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
zipfile
✓ import sys
if sys.version_info >= (3, 6):
import zipfile
else:
import zipfile36 as zipfile
✗ import zipfile36
The backport is designed to be aliased as `zipfile` for compatibility with existing code written for the standard library's `zipfile` module. Direct import as `zipfile36` works but deviates from the intended usage pattern.
This quickstart demonstrates how to use `zipfile36` (aliased as `zipfile`) to create a ZIP archive, add a file to it, and then read the file's contents from the archive. It includes the recommended conditional import statement.
import sys
import os
import io
# Suggested usage for zipfile36 backport
if sys.version_info >= (3, 6):
import zipfile
else:
try:
import zipfile36 as zipfile
except ImportError:
print("zipfile36 not installed. Please install with 'pip install zipfile36' for Python versions < 3.6.")
sys.exit(1)
# Create a dummy file
file_content = "Hello, zipfile36! This is a test file."
file_name = "test_file.txt"
with open(file_name, "w") as f:
f.write(file_content)
zip_file_name = "my_archive.zip"
# 1. Create a zip archive and add a file
print(f"Creating '{zip_file_name}' and adding '{file_name}'...")
with zipfile.ZipFile(zip_file_name, 'w', zipfile.ZIP_DEFLATED) as zf:
zf.write(file_name, arcname="docs/zipped_test_file.txt")
print(f"'{file_name}' added to '{zip_file_name}' as 'docs/zipped_test_file.txt'.")
# 2. Read from the zip archive
print(f"\nReading contents from '{zip_file_name}'...")
with zipfile.ZipFile(zip_file_name, 'r') as zf:
print(f"Files in '{zip_file_name}': {zf.namelist()}")
with zf.open("docs/zipped_test_file.txt") as member_file:
read_content = member_file.read().decode('utf-8')
print(f"Content of 'docs/zipped_test_file.txt': {read_content}")
# Clean up
os.remove(file_name)
os.remove(zip_file_name)
print("\nCleaned up dummy files.")
Debug
Known issues
breakingPickling `zipfile.ZipFile` instances will raise a `TypeError` in Python 3.6+ (and by extension, when using `zipfile36`). This behavior change fixed a prior bug in Python < 3.6 where `ZipFile` objects containing unpicklable `_thread.RLock` objects were incorrectly picklable. Code relying on pickling `ZipFile` objects for serialization will break.fixAvoid pickling `zipfile.ZipFile` instances directly. Instead, serialize the necessary file paths or contents and recreate `ZipFile` objects as needed.
affects: 0.1.x on Python >= 3.6 (native `zipfile`), or older Python versions using `zipfile36`.
gotchaThe `zipfile.is_zipfile()` function is not a robust file type identifier and can produce false positives. It primarily checks for the presence of a ZIP central directory signature at the end of a file, which can coincidentally appear in other file types (e.g., gzipped files, executables with appended data).fixFor reliable file type detection, use dedicated file-sniffing libraries (e.g., `python-magic`, `filetype`) or validate the content more thoroughly after initial checks, especially when dealing with untrusted sources.
affects: All versions of `zipfile36` (and the native `zipfile` module in CPython).
breakingError types for certain operations changed in Python 3.6. Specifically, operations on a closed `ZipFile` (e.g., `open()`, `write()`, `extract()`, `extractall()`, `writestr()`) or with an invalid mode will now raise `ValueError` instead of `RuntimeError`.fixUpdate exception handling blocks to catch `ValueError` instead of `RuntimeError` for operations that misuse `ZipFile` objects (e.g., operating on a closed archive).
affects: 0.1.x, impacts users migrating from Python < 3.6 or expecting `RuntimeError`.
deprecatedThe `mode='U'` option for `ZipFile.open()` (for universal newlines mode) was removed in Python 3.6.fixInstead of `mode='U'`, use `io.TextIOWrapper` to read compressed text files with explicit encoding and newline handling. For example: `io.TextIOWrapper(zip_file.open('member.txt'), encoding='utf-8', newline='')`. affects: 0.1.x, specifically affects usage of `mode='U'`.
Upgrade
Version history
0.1.3latest on PyPI · released Oct 16, 2016
Audit
Dependencies
zliboptionalRequired for DEFLATE compression. Typically built-in with Python, but might be missing in some custom installations.
bz2optionalRequired for BZIP2 compression, if used.
lzmaoptionalRequired for LZMA compression, if used.
compression.zstdoptionalRequired for ZSTD compression, if used (available in newer Python versions).