简体   繁体   中英

How do I unzip a password protected zip file using Python

Below is the code.

from zipfile import ZipFile 

file_name = "XYZ.zip"

with ZipFile(file_name, 'r') as zip: 
    zip.printdir()
    print('Extracting all the files now...') 
    zip.extractall(pwd=b'123123$SADMK6%002#')
    print('Done!')

This throws an error:

NotImplementedError: compression type 99

The works file when its not password protected. Its only when I try to unzip the password protected file is when I get this error.

Any help would be greatly appreciated

You can use 7zip or other zip file extractor. there is different type of zip encryption so you can only unzip if the encryption method is same.

first install 7zip and add it's path to the environment variable on windows. Then run this code.

This piece of code helped me:

import subprocess
passwd = "asdf"
file_name = "F:\\test.zip"
output_path = "F:\\test"
subprocess.call(["7z", "x", '-p{}'.format(passwd), file_name,"-o"+output_path])

If you rather prefer a solution within Python, you can use this code instead

from zipfile import ZipFile 

file_name = "XYZ.zip"
with ZipFile(file_name, "r") as zip:
    zip.extractall(path="uncompressed", pwd="password".encode("utf-8"))

Alternatively, you can also tranfer the password in binary format - I am not sure why your code did not work, it succeeded for me in both cases (of course, do not forget to replace password ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM