I am trying to send a password protected zip file as a base64 string.
data = BytesIO()
zip = zipfile.ZipFile(data, 'w')
zip.writestr('test.csv', 'Hello, World')
zip.setpassword(b'1234')
zip.close()
b64zip = base64.b64encode(data.getvalue()).decode('utf-8')
This b64zip
variable is then parsed as an email attachment.
However, when I try to unzip the zip it doesn't prompt for a password. Was using this thread for reference: zipfile: how to set a password for a Zipfile?
How can I create a password protected zip file as a base64 string?
zipfile
module's documentation states, it only supports decryption of encrypted files in ZIP archives, and currently cannot create an encrypted file. Callingsetpassword()
only sets a default password to use when extracting encrypted files (and it warns that decryption is extremely slow). You will have to encrypt the files in the ZIP archive by some other means.