Skip to content Skip to sidebar Skip to footer

Stream A Large File From Url Straight Into A Gzip File

I want to stream a large file into a gzip file directly, instead of downloading it all into memory and then compressing. This is how far I have gotten (does not work). I know how t

Solution 1:

Alright I figured it out:

with requests.get(url, stream=True, verify=False) as r:
    if save_file_path.endswith('gz'):
        compressor = zlib.compressobj(9, zlib.DEFLATED, zlib.MAX_WBITS | 16)
        with open(save_file_path, 'wb') as f:
            for chunk in r.iter_content(chunk_size=1024*1024):
                f.write(compressor.compress(chunk))
            f.write(compressor.flush())
    else:
        with open(save_file_path, 'wb') as f:
            shutil.copyfileobj(r.raw, f)

Post a Comment for "Stream A Large File From Url Straight Into A Gzip File"