remotezip2

0.0.2 · active · verified Thu Apr 16

remotezip2 is a Python library that provides efficient access to individual files within a remote ZIP archive without requiring the full download of the entire archive. It functions by leveraging HTTP Range requests, making it suitable for scenarios where only specific contents of large remote ZIP files are needed. As a fork of the original `python-remotezip`, it aims for continued maintenance and responsiveness. The current version is 0.0.2, with a relatively slow release cadence, having received a maintenance update in late 2024 since its initial release.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to list the contents of a remote ZIP file and extract a specific file using `remotezip2`. It initializes `RemoteZip` with a URL and then uses `namelist()` to see the files and `open()` to read the content of a specific member. The `REMOTE_ZIP_URL` environment variable can be used to specify a different ZIP file for testing.

import os
from remotezip2 import RemoteZip

# Replace with a URL to a real remote ZIP file that supports HTTP Range headers
# For testing, you can use a publicly available ZIP, e.g., one from thematicmapping.org
zip_url = os.environ.get('REMOTE_ZIP_URL', 'http://thematicmapping.org/downloads/TM_WORLD_BORDERS-0.3.zip')

try:
    with RemoteZip(zip_url) as rz:
        print(f"Files in remote ZIP at {zip_url}:")
        for name in rz.namelist():
            print(f" - {name}")

        # Example: Extract a specific file
        file_to_extract = 'Readme.txt'
        if file_to_extract in rz.namelist():
            print(f"\nExtracting '{file_to_extract}'...")
            with rz.open(file_to_extract) as remote_file:
                content = remote_file.read().decode('utf-8')
                print(f"Content of '{file_to_extract}':\n---\n{content[:200]}...\n---") # Print first 200 chars
        else:
            print(f"\nFile '{file_to_extract}' not found in the archive.")

except Exception as e:
    print(f"An error occurred: {e}")
    print("Please ensure the URL is valid and the server supports HTTP Range requests.")

view raw JSON →