'Writing BytesIO objects to in-memory Zipfile

I have a Flask-based webapp that I'm trying to do everything in-memory without touching the disk at all.

I have created an in-memory Word doc (using python-docx library) and an in-memory Excel file (using openpyxl). They are both of type BytesIO. I want to return them both with Flask, so I want to zip them up and return the zipfile to the user's browser.

My code is as follows:

inMemory = io.BytesIO()
zipfileObj = zipfile.ZipFile(inMemory, mode='w', compression=zipfile.ZIP_DEFLATED)
    try:
        print('adding files to zip archive')
        zipfileObj.write(virtualWorkbook)
        zipfileObj.write(virtualWordDoc)

When the zipfile tries to write the virtualWorkbook I get the following error: {TypeError}stat: path should be string, bytes, os.PathLike or integer, not BytesIO

I have skimmed the entirety of the internet but have come up empty-handed, so if someone could explain what I'm doing wrong that would be amazing



Solution 1:[1]

Seems like it's easier to mount tmpfs/ramdisk/smth to a specific directory like here, and just use tempfile.NamedTemporaryFile() as usual.

Solution 2:[2]

You could use the writestr method. It accepts both string and bytes.

zipfileObj.write(zipfile.ZipInfo('folder/name.docx'),
                                  virtualWorkbook.read())

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 madbird
Solution 2 fgui