Python error: "That compression method is not supported"

Viewed 36

I am trying to decompress some .zip or .rar archives, and i am getting the error "That Compression methond is not supported". All the files from this directory are .zip files.

enter image description here

import rarfile
import sys
import os, zipfile
from tkinter import *
from tkinter import filedialog
from tkinter import messagebox

ZipExtension='.zip'
RarExtension='.rar'
#filesZIP="..\directory"

try:
    os.chdir(filesZIP) # change directory from working dir to dir with files
except:
    messagebox.showerror("Error","The folder with the archives was not selected! Please run the app again and select the folder.")
    sys.exit()


for item in os.listdir(filesZIP):# loop through items in dir
    if item.endswith(ZipExtension): # check for ".zip" extension
        file_name = os.path.abspath(item) # get full path of files
        zip_ref = zipfile.ZipFile(file_name) # create zipfile object
        zip_ref.extractall(filesZIP) # extract file to dir
        zip_ref.close() # close file

for item in os.listdir(filesZIP):
    if item.endswith(RarExtension):
        file_name = os.path.abspath(item)
        rar_ref = rarfile.RarFile(file_name)
        rar_ref.extractall()
        rar_ref.close()


messagebox.showinfo("Information",'Successful!')

The problem is that sometimes it works, and in some cases, like the one above, it gives me that error, even though there are all .zip files, with no password

1 Answers

Background

By design zip archives support at lot of different compression methods. The support for these different compression methods in python varies depending on the version of the zipfile library you are running.

With Python 2.x, I see zipfile supports only deflate and store

zipfile.ZIP_STORED
The numeric constant for an uncompressed archive member.

zipfile.ZIP_DEFLATED
The numeric constant for the usual ZIP compression method. This requires the zlib module. No other compression methods are currently supported.

while with Python 3, zipfile supports a few more

zipfile.ZIP_STORED
The numeric constant for an uncompressed archive member.

zipfile.ZIP_DEFLATED
The numeric constant for the usual ZIP compression method. This requires the zlib module.

zipfile.ZIP_BZIP2
The numeric constant for the BZIP2 compression method. This requires the bz2 module.

New in version 3.3.

zipfile.ZIP_LZMA
The numeric constant for the LZMA compression method. This requires the lzma module

What Compression Methods are being used?

To see if this is your issue, you first need to see what compression method is actually being used in your zip files.

Let me work though an example to see how that works.

First create a zip file using bzip2 compression

zip -Z bzip2 /tmp/try.zip /tmp/in.txt

Let's check what unzip can tell us about the compression method it actually used.

$ unzip -lv try.zip 
Archive:  try.zip
 Length   Method    Size  Cmpr    Date    Time   CRC-32   Name
--------  ------  ------- ---- ---------- ----- --------  ----
  387776  BZip2     30986  92% 2022-09-20 14:11 f3d1fbaf  in.txt
--------          -------  ---                            -------
  387776            30986  92%                            1 file

In unzip the Method column says it is using Bzip2 compression. I'm sure that WinZip has an equivalent report.

Unzip with Python 2.7

Next try uncompressing this zip file with Python 2.7 - I'll use the code below with Python 2 & Python 3

import zipfile

zip_ref = zipfile.ZipFile('/tmp/try.zip') 
if zip_ref.testzip() is None:
    print("zip file is ok")
zip_ref.close() 

First Python 2.7 -- that matches what you are seeing. So that confirms that zipfile with Python 2.7 doesn't support bziip2 compression.

$ python2.7 /tmp/z.py
Traceback (most recent call last):
  File "/tmp/z.py", line 4, in <module>
    if zip_ref.testzip() is None:
  File "/usr/lib/python2.7/zipfile.py", line 921, in testzip
    with self.open(zinfo.filename, "r") as f:
  File "/usr/lib/python2.7/zipfile.py", line 1033, in open
    close_fileobj=should_close)
  File "/usr/lib/python2.7/zipfile.py", line 553, in __init__
    raise NotImplementedError("compression type %d (%s)" % (self._compress_type, descr))
NotImplementedError: compression type 12 (bzip2)

Unzip with Python 3.10

Next with Python 3.10.

$ python3.10 /tmp/z.py
zip file is ok

As expected, all is fine in this instance -- zipdetails with Python 3 does support bzip2 compression.

Related