Hi
I have saved a zip file made up of folders and files to viktor storage.
i now want to extract specific files from the stored file.
when it was a zip file i could quite simply use zipfile.ZipFile, however it is now a viktor.File object (typically considered to be a single file.).
this is probably a more general python question, but can anyone give me some guidance.
for note, the files of interest in the zip file are json files.
Thanks in advance
Hi Natalie,
Thanks for your question. The VIKTOR File
object can be converted to bytes or strings by using the getvalue
and getvalue_binary
method respectively. Knowing that, you can look into the ZipFile
documentation how to convert bytes to a ZipFile
. After a quick Google search I found this StackOverflow link:
Taking the answer presented there, this is how you will probably convert a VIKTOR File
object to a ZipFile
object:
import zipfile
import io
from viktor import File, Storage
storage = Storage()
my_file_from_storage = storage.get('data_key_1', scope='entity')
z = my_file_from_storage.getvalue_binary()
zf = zipfile.ZipFile(io.BytesIO(z), "r")
for fileinfo in zf.infolist():
print(zf.read(fileinfo).decode('ascii'))
Let me know if this works.
It works but seems to time out when looping for multiple zip files. but the theory works well for a single file