-1

I have a zip file that I can only unzip using terminal (on mac). I have not tried windows. For reference, I tried opening the file by double clicking in the finder window and I get "Error - 79 Inappropriate file type or format"

But this command on terminal works as expected:

unzip zip_file.zip > extracted.txt

My final goal is to extract this file using python 3.x I have tried

with py7zr.SevenZipFile(fq_file_name, mode='r') as archive:
    archive.extractall(file_path)

Error:

    raise Bad7zFile("not a 7z file")
py7zr.exceptions.Bad7zFile: not a 7z file

With this:

with zipfile.ZipFile(fq_file_name, 'r') as zip_ref:
    zip_ref.extractall(file_path)

Error:

raise NotImplementedError("That compression method is not supported")
NotImplementedError: That compression method is not supported

I even tried shutil

shutil.unpack_archive(fq_file_name)

Error

NotImplementedError: That compression method is not supported 

Looking inside the zipfile module, it's failing because the file requests compression method 9, which it doesn't support. Apparently method 9 is DEFLATE64. Is there any way to compress this file in Python?

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
Bitmask
  • 918
  • 2
  • 11
  • 22
  • Don't just tell us what you tried; tell us _specifically_ how each attempt failed. If we don't know the specific fault, we can't say how to fix it. – Charles Duffy Apr 12 '23 at 22:57
  • If you're going to provide the header, it'd be a lot more helpful to get a hex dump than raw characters as your terminal interpreted them; `xxd – Charles Duffy Apr 12 '23 at 22:58
  • I think its a valid zip file because I can unzip using the command on the terminal – Bitmask Apr 12 '23 at 23:00
  • Ahh, the compression method not supported detail is an important one. Of course, it'd be better if the exception told us which method it _was_ that's being requested and wasn't available... but you could probably use a debugger to look at the point where the exception is raised and figure that out. – Charles Duffy Apr 13 '23 at 10:44
  • 1
    digging in .. looks like the compression method is 9. zipfile does not support it? Is there any Py lib that supports it? – Bitmask Apr 13 '23 at 15:49
  • ...okay, a quick Google makes it look like that's DEFLATE64. zlib is what most open source software uses for the deflate operation, and https://zlib.net/zlib_faq.html#faq40 describes why they choose not to support it (short form, too much work to try to reverse-engineer a proprietary algorithm when it isn't actually very good in the first place). – Charles Duffy Apr 13 '23 at 16:00
  • Do you _really_ need a Python-native implementation? If I were in your shoes, I'd probably shell out to `unzip`, maybe one file at a time in pipe mode if you wanted to process things in-memory. – Charles Duffy Apr 13 '23 at 16:01
  • that said, here you are: https://pypi.org/project/zipfile-deflate64/ – Charles Duffy Apr 13 '23 at 16:01

2 Answers2

0

On Jupyter you can unzip files using a bash command like this:

!unzip file.zip

If you want to unzip it into an especific folder you can add the folder name like this:

!unzip file.zip -d folderName

I haven't tried it on Mac, but I hope it works

  • 1
    This is what one would write as `subprocess.run(['unzip', 'file.zip'])`, but that's hardly doing it "in Python"; it's just using the external tool. (`!` doesn't work in standard Python; it's an IPython/Jupyter extension) – Charles Duffy Apr 12 '23 at 23:00
0

This file is using a compression form proprietary to pkware.

The library https://pypi.org/project/zipfile-deflate64/ implements support; with it installed, you can run:

import zipfile_deflate64 as zipfile
Charles Duffy
  • 280,126
  • 43
  • 390
  • 441