Python,
pasted
on Jun 9:
|
import os, sys, zipfile, optparse, zlib, fnmatch, time
optp=optparse.OptionParser(usage="%prog [options] <target-zip> <source-dir> [<source-dir...>]")
optp.add_option("-x", help="exclude mask", action="append", dest="exclude", default=[])
err=optp.error
options, args = optp.parse_args()
if len(args)<2:
optp.print_usage()
sys.exit(1)
zf=zipfile.ZipFile(args[0], "w")
def zfAddNullFile(zf, arcname, date_time):
arcname = os.path.normpath(os.path.splitdrive(arcname)[1])
arcname = arcname.lstrip(os.sep + os.altsep)
zinfo = zipfile.ZipInfo(arcname, date_time)
zinfo.external_attr = 0
zinfo.compress_type = zf.compression
zinfo.file_size = 0
zinfo.flag_bits = 0x00
zinfo.header_offset = zf.fp.tell() # Start of header bytes
zf._writecheck(zinfo)
zf._didModify = True
zinfo.CRC = CRC = zlib.crc32("")
zinfo.compress_size = 0
zinfo.file_size = 0
zf.fp.write(zinfo.FileHeader())
zf.filelist.append(zinfo)
zf.NameToInfo[zinfo.filename] = zinfo
for sourceDir in args[1:]:
for dp, dn, fn in os.walk(sourceDir):
for f in fn:
f=os.path.join(dp, f)
ok=True
for xmask in options.exclude:
if fnmatch.fnmatch(f, xmask):
ok=False
break
if ok:
mtime=time.localtime(os.stat(f).st_mtime)
print f
zfAddNullFile(zf, f, (mtime.tm_year, mtime.tm_mon, mtime.tm_mday, mtime.tm_hour, mtime.tm_min, mtime.tm_sec))
|
Output:
|
Traceback (most recent call last):
Line 1, in <module>
import os, sys, zipfile, optparse, zlib, fnmatch, time
ImportError: libz.so.1: cannot open shared object file: No such file or directory
|
|