Python ZipFile Package Guide | Generated by AI

Home 2025.10

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:

  1. Open a ZIP file for reading:
    with zipfile.ZipFile('example.zip', 'r') as zip_ref:
        # 'r' is for read mode
    
  2. List contents:
    with zipfile.ZipFile('example.zip', 'r') as zip_ref:
        file_list = zip_ref.namelist()  # Returns list of file names
        print(file_list)
    
  3. Extract files:
    • Extract all: zip_ref.extractall('destination_folder')
    • Extract one: zip_ref.extract('file_inside.zip', 'path')
  4. 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:

  1. Create a new ZIP file:
    with zipfile.ZipFile('new_archive.zip', 'w') as zip_ref:
        # 'w' is for write mode (overwrites if exists)
    
  2. 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().
  3. 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.

  4. Append to existing ZIP (use ‘a’ mode instead of ‘w’).

Common Operations and Tips

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!


Back

x-ai/grok-code-fast-1

Donate