zipfile-deflate64

0.2.0 · active · verified Tue Apr 14

zipfile-deflate64 is a Python library that extends the standard `zipfile` module to enable extraction of ZIP archives compressed with the Deflate64 algorithm. This is particularly useful for handling large ZIP files (>2GB) created by tools like Microsoft Windows Explorer, which often use Deflate64 compression. It is currently at version 0.2.0 and has a release cadence driven by bug fixes and Python version compatibility updates.

Warnings

Install

Imports

Quickstart

After installation, simply importing `zipfile_deflate64` (or aliasing it as `zipfile`) patches the standard library's `zipfile` module, allowing it to correctly handle Deflate64 compressed archives using its familiar API.

import os
import zipfile
import zipfile_deflate64 as custom_zipfile # This line enables Deflate64 support

# Create a dummy (standard Deflate) zip file for demonstration
dummy_zip_path = 'my_test_archive.zip'
with zipfile.ZipFile(dummy_zip_path, 'w', zipfile.ZIP_DEFLATED) as zf:
    zf.writestr('file1.txt', 'Hello from file 1!')
    zf.writestr('folder/file2.txt', 'Content for file 2.')

# Directory to extract to
extract_dir = 'extracted_content'
os.makedirs(extract_dir, exist_ok=True)

try:
    # Use the patched zipfile API to extract
    # This would work for both standard and Deflate64 archives after the import
    with custom_zipfile.ZipFile(dummy_zip_path, 'r') as zip_ref:
        zip_ref.extractall(extract_dir)
    print(f"Successfully extracted '{dummy_zip_path}' to '{extract_dir}'")

    # Verify extraction
    print(f"Content of {os.path.join(extract_dir, 'file1.txt')}: ")
    with open(os.path.join(extract_dir, 'file1.txt'), 'r') as f:
        print(f.read())

except Exception as e:
    print(f"An error occurred: {e}")
finally:
    # Clean up dummy files
    if os.path.exists(dummy_zip_path):
        os.remove(dummy_zip_path)
    if os.path.exists(os.path.join(extract_dir, 'file1.txt')):
        os.remove(os.path.join(extract_dir, 'file1.txt'))
    if os.path.exists(os.path.join(extract_dir, 'folder', 'file2.txt')):
        os.remove(os.path.join(extract_dir, 'folder', 'file2.txt'))
    if os.path.exists(os.path.join(extract_dir, 'folder')):
        os.rmdir(os.path.join(extract_dir, 'folder'))
    if os.path.exists(extract_dir):
        os.rmdir(extract_dir)

view raw JSON →