def create_zipfile(source,target,get_zipinfo=None,members=None,compress=None):
    if not compress:
        compress_type = zipfile.ZIP_STORED
    else:
        compress_type = zipfile.ZIP_DEFLATED
    zf = zipfile.ZipFile(target,"w",compression=compress_type)
    if members is None:
        def gen_members():
            for (dirpath,dirnames,filenames) in os.walk(source):
                for fn in filenames:
                    yield os.path.join(dirpath,fn)[len(source)+1:]
        members = gen_members()
    for fpath in members:
        if isinstance(fpath,zipfile.ZipInfo):
            zinfo = fpath
            fpath = os.path.join(source,zinfo.filename)
        else:
            if get_zipinfo:
                zinfo = get_zipinfo(fpath)
            else:
                zinfo = None
            fpath = os.path.join(source,fpath)
        if os.path.islink(fpath):
            # For information about adding symlinks to a zip file, see
            # https://mail.python.org/pipermail/python-list/2005-June/322180.html
            dest = os.readlink(fpath)
            if zinfo is None:
                zinfo = zipfile.ZipInfo()
                zinfo.filename = fpath[len(source)+1:]
            elif isinstance(zinfo,basestring):
                link = zinfo
                zinfo = zipfile.ZipInfo()
                zinfo.filename = link
            else: # isinstance(zinfo,zipfile.ZipInfo)
                pass
            zinfo.create_system = 3
            zinfo.external_attr = 2716663808L # symlink: 0xA1ED0000
            zf.writestr(zinfo,dest)
        else: # not a symlink
            if zinfo is None:
                zf.write(fpath,fpath[len(source)+1:])
            elif isinstance(zinfo,basestring):
                zf.write(fpath,zinfo)
            else:
                with open(fpath,"rb") as f:
                    zf.writestr(zinfo,f.read())
    zf.close()
Bundle the contents of a given directory into a zipfile.
dataset/ETHPy150Open cloudmatrix/esky/esky/util.py/create_zipfile