why can't python unzip a password protected zip file created by winrar using the zip method?
Asked Answered
T

3

11

I have searched the web high and low but still couldn't find a solution for the above problem. Does anyone out there know why and if so how it can be done?

psw="dg"

ZipFile.extractall("data.zip", None, psw)

The error that I've got:

TypeError: unbound method extractall() must be called
with ZipFile instance as first argument (got str instance instead)
Territerrible answered 16/8, 2014 at 4:10 Comment(3)
Documentation here: docs.python.org/2/library/zipfile.html, see ZipFile.open(name[, mode[, pwd]])Glass
Thanks for your response but though the document says this it does not work that way and I can't find a single example anywhere on the web. It is strange! It is as if no one ever zipped a password protected file using the zip method in python and if they did never spoke about it!Territerrible
actually, @Glass gave you perfectly correct link. :) You did not read documentation properly, that's the issue. See my answer below, please.Womanhood
W
34

Because you are using it wrong. :) From docs:

ZipFile.extractall([path[, members[, pwd]]])

Extract all members from the archive to the current working directory. path specifies a different directory to extract to. members is optional and must be a subset of the list returned by namelist(). pwd is the password used for encrypted files.

So you should call that this function for ZipFile object, not as static method. And you should not pass name of archive as a first argument. :)

this way it'll work:

from zipfile import ZipFile

with ZipFile('data.zip') as zf:
    zf.extractall(pwd='dg')

EDIT, in newer versions use:

zf.extractall(pwd=b'dg')
Womanhood answered 16/8, 2014 at 9:29 Comment(4)
Thank You, the above worked and I accept your answer. But I noticed that the extractall method took quite some time to finish. Around 5 minutes to be exact. I zipped 8 files consisting of bmp, png, jpeg, wav, ogg and mp3 all of a total size of 10.7Mb . Why is that? I have had to wait for so long on such a small size. Would like to know more on this. :)Territerrible
An answer to @wookie's old question of why ZipFile is so slow: The documentation states Decryption is extremely slow as it is implemented in native Python rather than C.Estrella
it actually zf.extractall(pwd=b'dg')Cykana
@LarrySong Thanks for the mentioning b. This is the correct way.Rhachis
I
4

To offer the exact syntaxt without acronym:

from zipfile import ZipFile

str_zipFile = 'FileZip.zip'
str_pwd= 'xxxx'

with ZipFile(str_zipFile) as zipObj:
  zipObj.extractall(pwd = bytes(str_pwd,'utf-8'))
Irvingirwin answered 2/7, 2020 at 4:20 Comment(0)
C
0

Some files do not know what they are from and have long extract.

For example, if it is not extracted for 60 seconds or the time is passed, I want to delete the original file to cancel the operation.

import pyzipper

with pyzipper.AESZipFile('test.zip', 'r', compression=pyzipper.ZIP_LZMA, encryption=pyzipper.WZ_AES) as extracted_zip:
        extracted_zip.extractall(pwd=str.encode("@apkclub"))
        os.remove(test.zip)
Coth answered 2/5, 2022 at 22:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.