Search code examples
python-3.xzip

Python ZipFile module problem when file is encrypted


I have the following short program

from zipfile import ZipFile
procFile1 ="C:\\Temp\\XLFile-Demo.zip"
procFile2 ="C:\\Temp2\\XLFile-Demo-PW123.zip"

# Unencrypted file
print ("Unencrypted file")
myzip1 = ZipFile(procFile1)
print (myzip1.infolist())
myzip1.extractall("C:\\Temp")

# Encrypted File
print ("Encrypted file")
myzip2 = ZipFile(procFile2)
print (myzip2.infolist())
myzip2.setpassword(bytes('123', 'utf-8'))
myzip2.extractall("C:\\Temp2")enter code here

At this Amazon Drive link are the two files. They are identical except that one zip is protected with the password 123.

Executing the above code successfully extracts the unencrypted one but raises the error NotImplementedError: That compression method is not supported for the other.

Unencrypted file
[<ZipInfo filename='XLFile-Demo.xlsx' compress_type=deflate external_attr=0x20 file_size=31964 compress_size=29252>]

Encrypted file
[<ZipInfo filename='XLFile-Demo.xlsx' compress_type=99 external_attr=0x20 file_size=31964 compress_size=29280>]

Am I doing anything wrong from my end?


Solution

  • The error came up when the file was zipped using WinRar's ZIP option. I installed 7Zip and it is working.

    The .infolist for the 7Zip file is the following:

    [<ZipInfo filename='XLFile-Demo.xlsx' compress_type=deflate external_attr=0x20 file_size=31964 compress_size=29340>]
    

    Incidentally WinRar can handle this file and 7Zip can correctly process the encrypted Zip archive created by WinRar.