File empty when using io.BytesIO()
Hey,
first of all, thank you for this amazing project :pray:
I'm currently facing following error: I want to do some ZIP creation in memory for a web application. Therefore, I'd like to use something like io.BytesIO() in order to return just a byte stream which can be downloaded from the web application. Code example:
import pyzipper
import io
def zip_func(filename, data):
secret_password = b'lost art of keeping a secret'
zip_buffer = io.BytesIO()
with pyzipper.AESZipFile(zip_buffer,
'w',
compression=pyzipper.ZIP_LZMA,
encryption=pyzipper.WZ_AES) as zf:
zf.setpassword(secret_password)
zf.writestr(filename, data)
return zip_buffer
# Writing it to disk in order to test function
fd = zip_func(hash_tmp, hash_tmp)
with open("test.zip", "wb") as f:
f.write(fd.getbuffer())
f.close()
Unfortunately, when creating the file, PW protection and file naming seem to work, but no file content is written.
When using zipfile instead, it seems to be working this way:
import zipfile
import io
def zip_func(filename, data):
zip_buffer = io.BytesIO()
with zipfile.ZipFile(zip_buffer,'w') as zf:
zf.writestr(filename, data)
return zip_buffer
fd = zip_func(hash_tmp, hash_tmp)
with open("test.zip", "wb") as f:
f.write(fd.getbuffer())
f.close()
File content is written as expected.
Thanks for helping me! :blush:
Hi Christoph,
How are you opening the encrypted zip that results in the empty file? Most OS's native zip programs can't decrypt AES zipped files. The behaviour on some Mac osx versions is to ask for a password and write out an empty file without an error (which sounds similar to your bug report). You'll need to install another program to be able to extract these files. 7zip is my recommendation. 7zip doesn't have a GUI on mac yet but 7zip's download page suggests https://www.keka.io as an open source GUI extractor for mac that can handle AES zip files.
I've extended your examples above to re-read the file using pyzipper and didn't encounter this bug:
import pyzipper
import zipfile
import io
hash_tmp = 'test_file.txt'
def zip_func(filename, data):
zip_buffer = io.BytesIO()
with zipfile.ZipFile(zip_buffer, 'w') as zf:
zf.writestr(filename, data)
return zip_buffer
fd = zip_func(hash_tmp, hash_tmp)
with open("test_native.zip", "wb") as f:
f.write(fd.getbuffer())
f.close()
secret_password = b'lost art of keeping a secret'
def zip_func_aes(filename, data, pword):
zip_buffer = io.BytesIO()
with pyzipper.AESZipFile(
zip_buffer,
'w',
compression=pyzipper.ZIP_LZMA,
encryption=pyzipper.WZ_AES
) as zf:
zf.setpassword(pword)
zf.writestr(filename, data)
return zip_buffer
aes_zip_fname = "test_aes.zip"
# Writing it to disk in order to test function
fd = zip_func_aes(hash_tmp, hash_tmp, secret_password)
with open(aes_zip_fname, "wb") as f:
f.write(fd.getbuffer())
f.close()
with pyzipper.AESZipFile(
aes_zip_fname,
compression=pyzipper.ZIP_LZMA,
encryption=pyzipper.WZ_AES
) as zf:
zf.setpassword(secret_password)
print(zf.namelist())
print(zf.read(hash_tmp))
Hey Daniel,
okay, seems like a typical Layer 8 problem :facepalm: I read the other issue with empty files, leading to an installation of 7za (Fedora Linux), but it looks like I opened the wrong ZIP or did something else weird :man_shrugging:
Nevertheless, can't reproduce it anymore. Unflating with pyzipper and 7za works flawlessly. Nautilus' ZIP integration is creating an empty file like you already described.
Thanks for your help!
No worries! All the best with your project :)