Skip to content

Instantly share code, notes, and snippets.

@gg7
Created March 26, 2019 18:46
Show Gist options
  • Save gg7/e23639e6dd54c314da6d2ca0ac3f2f66 to your computer and use it in GitHub Desktop.
Save gg7/e23639e6dd54c314da6d2ca0ac3f2f66 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
import argparse
import logging
import os
import zipfile
logger = logging.getLogger(__name__)
def main(): # type: () -> None
logging.basicConfig(level=logging.DEBUG)
parser = argparse.ArgumentParser()
parser.add_argument("zipfile")
args = parser.parse_args()
src = args.zipfile
dst = args.zipfile + ".new"
if os.path.exists(dst):
raise Exception("Destination file (%r) already exists" % (dst,))
logger.debug("Reading %r", src)
with zipfile.ZipFile(src, 'r') as zf_src, zipfile.ZipFile(dst, 'w') as zf_dst:
for info in zf_src.infolist():
info_data = zf_src.read(info.filename)
logger.debug("chmod a+r %r", info.filename)
# From https://unix.stackexchange.com/questions/14705/the-zip-formats-external-file-attribute/14727#14727
# TTTTsstrwxrwxrwx0000000000ADVSHR
# ^^^^____________________________ file type as explained above
# ^^^_________________________ setuid, setgid, sticky
# ^^^^^^^^^________________ permissions
# ^^^^^^^^________ This is the "lower-middle byte" your post mentions
# ^^^^^^^^ DOS attribute bits
info.external_attr |= 0444 << 16
zf_dst.writestr(info, info_data)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment