]> git.phdru.name Git - dotfiles.git/blob - bin/zip.py
Initial import
[dotfiles.git] / bin / zip.py
1 #! /usr/bin/env python
2 """Zip (zip -r9) with encoded filenames
3
4    Written by Oleg Broytman. Copyright (C) 2009, 2010 PhiloSoft Design.
5 """
6
7 import sys, os
8 from getopt import getopt, GetoptError
9 from zipfile import ZipFile, ZIP_DEFLATED
10 from m_lib.defenc import default_encoding
11
12 def usage():
13     sys.exit('Usage: %s file.zip file1 dir2...' % sys.argv[0])
14
15 try:
16   options, arguments = getopt(sys.argv[1:], '')
17 except GetoptError:
18     usage()
19
20 if len(arguments) < 2:
21     usage()
22
23 def addToZip(zf, path):
24     if os.path.isfile(path):
25         print path
26         recoded_path = path.decode(default_encoding).encode('cp866')
27         zf.write(path, recoded_path, ZIP_DEFLATED)
28     elif os.path.isdir(path):
29         for nm in os.listdir(path):
30             addToZip(zf, os.path.join(path, nm))
31     # else: ignore
32
33 zname = arguments[0]
34 if os.path.splitext(zname)[1] not in ('.zip', '.ZIP'):
35     zname += '.zip'
36
37 zf = ZipFile(zname, 'w', allowZip64=True)
38
39 for path in arguments[1:]:
40     addToZip(zf, path)
41
42 zf.close()