I'd like to zip a list of opened files in python. Libraries such as ZipFile and shutil can compress files only if you specify the path to them, not giving them the file itself. I'd like to avoid a workaround like saving each file and reading it with ZipFile or shutil, as some files may be quite big and this would be time consuming.
Using ZipFile.writestr
seems to achieve this (also see documentation)
from zipfile import ZipFile
with ZipFile('spam.zip', 'w') as myzip:
myzip.writestr('test.txt', 'test')
zip_file = ZipFile('spam.zip', 'r')
zip_file.namelist()
['test.txt']
From the source code it does not appear to save to a temp file, but writes directly to the zip archive.