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?

8
  • I tried your code and it worked for me, can you unzip it with another program with the same password? Commented May 30, 2020 at 18:51
  • Why is the filename in the exception 'NegSkew.pdf'? Commented May 30, 2020 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 Commented May 30, 2020 at 18:57
  • 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 <ZipInfo filename='sample.txt' ...>. 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. Commented May 30, 2020 at 19:16
  • 1
    @liorr The exception shows the name of the file that was to be extracted from the zip Commented May 30, 2020 at 19:42

1 Answer 1

3

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')) 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.