I'm trying to create a zip file on the fly of a directory and return it to the user over a Flask App. The below works great for smaller directories but I'd also like to achieve this with large directories full of images (>20GB).
def return_zip():
dir_to_send = '/dir/to/the/files'
base_path = pathlib.Path(dir_to_send)
data = io.BytesIO()
with zipfile.ZipFile(data, mode='w') as z:
for f_name in base_path.iterdir():
z.write(f_name, arcname=f_name.name)
data.seek(0)
return send_file(data, mimetype='application/zip', as_attachment=True, attachment_filename='data.zip')
When trying this on large directories the whole system crashes, is there any way to create zips in stream that exceed the system's memory?
I'd prefer not to write the ZIP to disk then send it to the user then delete it from disk as this just increases the R/W operations to wear out the storage drive everything is located on.
The OS is running on an SSD (not the same drive as the images to zip), maybe part of this could be turned into virtual RAM? I'm not very adept at working in memory.
Any ideas would be greatly appreciated!
Ubuntu 20.04, Python3 with Flask, 2TB storage drive and a 250GB OS SSD with 8GB RAM.