10/27/2019

Python: Create in Memory Zip File

simple example to make zip in memory

..
import io
import zipfile

def makeZip(data_list):
    zip_buffer = io.BytesIO()
    with zipfile.ZipFile(zip_buffer, "a", zipfile.ZIP_DEFLATED, Falseas zip_file:
        for file_name, data in data_list:
            zip_file.writestr(file_name, data.getvalue())
    return zip_buffer

data_list = [('1.txt', io.BytesIO(b'111')), ('2.txt', io.BytesIO(b'222'))]
zip_buffer = makeZip(data_list)
with open('./b.zip''wb'as f:
    f.write(zip_buffer.getvalue())
..

Thank you.

No comments:

Post a Comment