'Unzipping an archive with zipfile turns an exe into text file (MacOS)

I'm using zipfile (Python 3.6) on a Mac to unzip a zip archive containing files, folders, and an executable. The executable was made with PyInstaller and zipped on the Mac. When I unzip the archive it transforms the executable from a Unix executable file type into a TextEdit file type. When I unzip manually everything works correctly and results in the desired Unix executable file. Everything works as expected on Windows.

I'm not sure how to post this with an example of the zip as I'm sure people would be hesitant to work with an unsigned exe file, but my code is below.

Note that I tried fixing the issue by using os.chmod to maybe alter permissions but that didn't work:

import zipfile
import os
zip_path = '/Applications/testzip/example.zip'
zip_dir = '/Applications/testzip'

zf = zipfile.ZipFile(zip_path)

for file in zf.infolist():
    path = os.path.join(file.filename, zip_dir)
    os.chmod(path, 0o0755)
    zf.extract(file.filename, zip_dir)

zf.close()
print('done')


Solution 1:[1]

Not sure if this is the best way to go about doing it, but I was able to get it working by using subprocess to change permissions after extraction. Seems to be very slow, but it works.

import zipfile
import subprocess
import os
zip_path = '/Applications/testzip/armada_pipeline.zip'
zip_dir = '/Applications/testzip'

zf = zipfile.ZipFile(zip_path)

for file in zf.infolist():
    path = os.path.join(zip_dir, file.filename)
    zf.extract(file, zip_dir)
    subprocess.call(['chmod', 'u+x', path])

zf.close()
print('done')

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Mike Bourbeau