#! /usr/bin/env python3 """Zip (zip -r9) with encoded filenames Written by Oleg Broytman. Copyright (C) 2009-2023 PhiloSoft Design. """ import sys, os from getopt import getopt, GetoptError from zipfile import ZipFile, ZIP_DEFLATED from m_lib.defenc import default_encoding def usage(): sys.exit('Usage: %s file.zip file1 dir2...' % sys.argv[0]) try: options, arguments = getopt(sys.argv[1:], '') except GetoptError: usage() if len(arguments) < 2: usage() def addToZip(zf, path): if os.path.isfile(path): print(path) if isinstance(path, bytes): recoded_path = path.decode(default_encoding).encode('cp866') else: recoded_path = path zf.write(path, recoded_path, ZIP_DEFLATED) elif os.path.isdir(path): for nm in os.listdir(path): addToZip(zf, os.path.join(path, nm)) # else: ignore zname = arguments[0] if os.path.splitext(zname)[1] not in ('.zip', '.ZIP'): zname += '.zip' zf = ZipFile(zname, 'w', allowZip64=True) for path in arguments[1:]: addToZip(zf, path) zf.close()