1

I'm writing a Python (2.7) script to build and distribute my Mac (10.7) app. I want to zip it up for distribution, but the following code results in an empty zipped app (0 bytes):

from zipfile import ZipFile

with ZipFile(zip_file_dist_path, 'w') as dist_zip:
    dist_zip.write(archived_app, os.path.basename(archived_app))

Is it treating it as a file, when I need to treat it as a directory? If I zip it up by walking the app bundle's directory tree and writing each file to the zip, will it unzip with all the proper file system metadata (i.e. will I get back out a proper app)?

Dov
  • 15,530
  • 13
  • 76
  • 177

2 Answers2

3

I switched from ZipFile to shutil.make_archive, since it takes care of the directory recursing, and I don't need any finer-grained control:

import shutil

zip_file_path = shutil.make_archive(zip_file_build_path, 'zip', root_dir = dist_dir);
Dov
  • 15,530
  • 13
  • 76
  • 177
  • @uchuugaka Couldn't you use the regular `ZipFile` class to unzip? – Dov Jan 14 '16 at 16:10
  • Doesn't seem to work at all on a Mac .app bundle, loses executable permissions. – uchuugaka Jan 15 '16 at 05:41
  • @uchuugaka I'm seeing `shutil.unpack_archive` in the documentation. Does that work for you? – Dov Jan 15 '16 at 17:11
  • No it did not. I tried it. What did work was `os.system("unzip -qo sourceFilePath -d tempDirPath")` (where the source and destination paths are set up prior in a joined string. – uchuugaka Jan 16 '16 at 09:15
1

ZipFile only zip one file to an archive once.

You can find how to adding folders to a zip file here: Adding folders to a zip file using python

Community
  • 1
  • 1
bcse
  • 11
  • 1