'Flask FileStorage file becomes empty after creating ZipFile object

I'm trying to save a .zip file received from an endpoint, but first check the contents of the .zip.

If I do:

import zipfile
from Flask import request

file = request.files["file"]
zf = zipfile.ZipFile(file, "r")
// some operation to check zf
file.save("path/to/save/file")

The saved file is empty. However, removing the construction of the ZipFile object, the file saved is not empty. Any advice on why this is the case?



Solution 1:[1]

Wanted to post the solution I found.

Adding

file.stream.seek(0)

to move the file pointer back to the beginning, before

file.save("path/to/save/file")

solves it.

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 bqiu