Skip to content

Instantly share code, notes, and snippets.

@Draknek
Last active March 27, 2024 15:21
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Draknek/3ce889860cea4f59838386a79cc11a85 to your computer and use it in GitHub Desktop.
Save Draknek/3ce889860cea4f59838386a79cc11a85 to your computer and use it in GitHub Desktop.
Normally if you zip a .app directory on Windows and unzip it on Mac OS X, it won't run. This tool creates a zip file that should have the correct executable flags set to work correctly.
#!/usr/bin/env python3
import os
import sys
import time
import zipfile
startingdir = os.getcwd()
if (len(sys.argv) == 1):
print("Usage: " + os.path.basename(sys.argv[0]) + " path/to/application.app [path/to/output.zip]")
sys.exit()
if (len(sys.argv) > 3):
print("Wrong number of arguments - needs 1 or 2")
sys.exit(1)
appdir = sys.argv[1]
if (appdir[-5:] == ".app/"):
appdir = appdir[0:-1]
if (appdir[-4:] != ".app"):
print("Error: " + appdir + " is not a .app file.")
sys.exit(2)
dir = os.path.dirname(appdir)
if (not os.path.exists(appdir)):
print("Error: " + appdir + " does not exist.")
sys.exit(3)
os.chdir(dir)
appdir = os.path.basename(appdir)
zipname = appdir[0:-4] + ".zip"
if (len(sys.argv) == 3):
zipname = sys.argv[2]
zippath = os.path.join(startingdir, zipname)
else:
zippath = zipname
searchname = os.path.join(appdir, "Contents", "MacOS")
zip = zipfile.ZipFile(zippath, 'w', zipfile.ZIP_DEFLATED)
for root, dirs, files in os.walk(appdir):
for file in files:
filename = os.path.join(root, file)
if (filename[0:len(searchname)] == searchname):
f = open(filename, 'rb')
bytes = f.read()
f.close()
info = zipfile.ZipInfo(filename)
info.date_time = time.localtime()
info.create_system = 3; # Pretend file is from unix
info.external_attr = 0o100755 << 16 # Set file permissions
zip.writestr(info, bytes, zipfile.ZIP_DEFLATED)
else:
zip.write(filename)
zip.close()
print("Created " + zipname)
@houkanshan
Copy link

Thanks Draknek! I ported it to python3 and made an exe file here: https://gist.github.com/houkanshan/3c02a8f4487b2d343ae23522c9abe4f0

@duolabmeng6
Copy link

Could you tell me how to keep the soft connection when decompressing?

@Draknek
Copy link
Author

Draknek commented Mar 27, 2024

Updated to python3, and fixed some issues (previously it seemed to be working on Windows from WSL but not otherwise)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment