0

I'm just doing my first steps with python 3 and tried to unzip a file which is protected by the password 'acb'. I'm able to unzip a files which isn't password protected but a nother zip files which has a password can't be unzipt.

I hope you can help me.

Code:

import zipfile
zipFile = zipfile.ZipFile(r'C:\Users\Desktop\pyth\test.zip') 
psw = 'acb'
zipFile.extractall(pwd=str.encode(psw))

Thanks for your help!

Edit: I also tried this but it didn't work for me as well

from zipfile import ZipFile
with ZipFile('test.zip') as zf:
    zf.extractall(pwd='acb')
Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
  • 2
    Does this answer your question? [why can't python unzip a password protected zip file created by winrar using the zip method?](https://stackoverflow.com/questions/25336859/why-cant-python-unzip-a-password-protected-zip-file-created-by-winrar-using-the) – Maurice Meyer Sep 10 '20 at 13:41
  • Hello and welcome to SO. Please take your time to properly format questions. – Daemon Painter Sep 10 '20 at 13:48
  • @MauriceMeyer That’s unrelated. According to the answers there, the code in this question should work. – Konrad Rudolph Sep 10 '20 at 20:23

1 Answers1

0

please try the following modified code:

just try to put the absolute path of the zip file and paste the error log next

import zipfile


def extractFile(z_File, password):
    try:
        pw_bytes = bytes(password,'utf-8')
        z_File.extractall(pwd=pw_bytes)
        #z_File.open()
        print(("[+] Found password " + password + "\n"))
    except:
        print("pass '"+ password + "' failed",end ="\r")
        pass

zname = r'C:\Users\Desktop\pyth\test.zip'
zFile = zipfile.ZipFile(zname)

extractFile(zFile,"abcd")

else we can try 7zip with subprocess also

subprocess.Popen([ r"C:\Program Files\7-Zip\7z.exe", "l", zipname, "-p{}".format(password)],
                                  stdout=subprocess.PIPE, stderr=subprocess.PIPE,stdin=subprocess.PIPE)
Vinod Srivastav
  • 3,644
  • 1
  • 27
  • 40