Downloading & reading a ZIP file in memory using Python

Problem:

You want to retrieve a ZIP file by downloading it from an URL in Python, but you don’t want to store it in a temporary file and extract it later but instead directly extract its contents in memory.

Solution

In Python3 can use io.BytesIO together with zipfile (both are present in the standard library) to read it in memory. The following example function provides a ready-to-use generator based approach on iterating over the files in the ZIP:

import requests
import io
import zipfile

def download_extract_zip(url):
    """
    Download a ZIP file and extract its contents in memory
    yields (filename, file-like object) pairs
    """
    response = requests.get(url)
    with zipfile.ZipFile(io.BytesIO(response.content)) as thezip:
        for zipinfo in thezip.infolist():
            with thezip.open(zipinfo) as thefile:
                yield zipinfo.filename, thefile