5

I created a zip file with Gnome Archive Manager (Ubuntu OS). I created the zip file with a password and I am trying to unzip it using the zipfile Python library:

import zipfile

file_name = '/home/mahmoud/Desktop/tester.zip'
pswd = 'pass'

with zipfile.ZipFile(file_name, 'r') as zf:
    zf.printdir()
    zf.extractall(path='/home/mahmoud/Desktop/testfolder', pwd = bytes(pswd, 'utf-8'))

When I run this code I get the following error and I am pretty sure that the password is correct. The error is:

File "/home/mahmoud/anaconda3/lib/python3.7/zipfile.py", line 1538, in open
  raise RuntimeError("Bad password for file %r" % name)

RuntimeError: Bad password for file <ZipInfo filename='NegSkew.pdf' compress_type=99 filemode='-rw-rw-r--' external_attr=0x8020 file_size=233252 compress_size=199427>

How can I unzip the file?

Dharman
  • 30,962
  • 25
  • 85
  • 135
Mahmoud Abdel-Rahman
  • 497
  • 2
  • 10
  • 27
  • I tried your code and it worked for me, can you unzip it with another program with the same password? – Nico Müller May 30 '20 at 18:51
  • Why is the filename in the exception 'NegSkew.pdf'? – liorr May 30 '20 at 18:55
  • I tried unzipping the file with winrar and 7z and both worked fine and the password is correct all of that is done on a windows machine @NicoMüller – Mahmoud Abdel-Rahman May 30 '20 at 18:57
  • @liorr do you think file permissions are the problem? I have pdf files that are zipped in the tester.zip – Mahmoud Abdel-Rahman May 30 '20 at 18:58
  • 2
    Hmm. This is an interesting question, and I am able to reproduce your observations. I am running on Ubuntu 18.04.4 LTS, with Python 3.7.5. I made a simple text file, zipped & encrypted it in the Gnome Archive Manager. Like yourself, when I try to perform `extractall`, I am seeing `Bad password for file `. This is definitely getting an up-vote from me, and I am going to poke at it a bit. I am inclined to think that Python's builtin ZIP library may not support the encryption algo used by Gnome's Archive Manager, but I don't have any thing to back that up yet. – Spencer D May 30 '20 at 19:16
  • 1
    @liorr The exception shows the name of the file that was to be extracted from the zip – Sam May 30 '20 at 19:42
  • This works on Mac OS X where I created a zipfile using `zip -e zipfile.zip inputfil.txt`. I set the password, and then used that password in your code above, and it worked perfectly. So yes, I suspect it is something about the encryption that GAM uses. – Sam May 30 '20 at 19:43
  • Zipping files with winrar and adding password, zipfile library won't open it. Zipping files with 7z and adding password and AES-256 encryption used, zipfile library won't open it. Zipping files with 7z and adding password and ZipCrypto encryption used, zipfile library will be able to extract it. And it looks like Gnome Archive Manager encrypts the zip file automatically when adding a password that is why zipfile library can't extract the files. – Mahmoud Abdel-Rahman May 30 '20 at 20:07

1 Answers1

2

zipfile library doesn't support AES encryption (compress_type=99) and only supports CRC-32 as mentioned in the _ZipDecrypter code (https://hg.python.org/cpython/file/a80c14ace927/Lib/zipfile.py#l508). _ZipDecrypter is called and used right before that particular RuntimeError is raised in ZipFile.open which can be traced from extractall.

You can use pyzipper library (https://github.com/danifus/pyzipper) instead of zipfile to unzip the file:

import pyzipper

file_name = '/home/mahmoud/Desktop/tester.zip'
pswd = 'pass'

with pyzipper.AESZipFile(file_name) as zf:
    zf.extractall(path='/home/mahmoud/Desktop/testfolder', pwd = bytes(pswd, 'utf-8'))