u''' Create zip file in memory. ''' classInMemoryZIP(object):
def__init__(self): # create the in-memory file-like object self.in_memory_zip = BytesIO()
defappend(self, filename_in_zip, file_contents): """ Appends a file with name filename_in_zip \ and contents of file_contents to the in-memory zip. """ # create a handle to the in-memory zip in append mode zf = zipfile.ZipFile(self.in_memory_zip, 'a', zipfile.ZIP_DEFLATED, False)
# write the file to the in-memory zip zf.writestr(filename_in_zip, file_contents)
# mark the files as having been created on Windows # so that Unix permissions are not inferred as 0000 for zfile in zf.filelist: zfile.create_system = 0 return self
defappendfile(self, file_path, file_name=None): """ Read a file with path file_path \ and append to in-memory zip with name file_name. """ if file_name isNone: file_name = os.path.split(file_path)[1]