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)
Dharman
  • 30,962
  • 25
  • 85
  • 135
wookie
  • 329
  • 1
  • 5
  • 13
  • 1
    Documentation here: https://docs.python.org/2/library/zipfile.html, see ZipFile.open(name[, mode[, pwd]]) – Stefan Aug 16 '14 at 06:56
  • 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! – wookie Aug 16 '14 at 07:21
  • actually, @Stefan gave you perfectly correct link. :) You did not read documentation properly, that's the issue. See my answer below, please. – Bruno Gelb Aug 16 '14 at 09:40

3 Answers3

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')
evandrix
  • 6,041
  • 4
  • 27
  • 38
Bruno Gelb
  • 5,322
  • 8
  • 35
  • 50
  • 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. :) – wookie Aug 17 '14 at 18:17
  • 4
    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.` – Klox Mar 10 '17 at 21:57
  • 6
    it actually zf.extractall(pwd=b'dg') – Larry Song Mar 21 '18 at 00:51
  • 1
    @LarrySong Thanks for the mentioning ```b```. This is the correct way. – nainometer Aug 17 '19 at 09:04
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'))
Laurent T
  • 275
  • 3
  • 7
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)
Rene
  • 976
  • 1
  • 13
  • 25
aziz n
  • 1