def create_zip(files, trim_arcname=None, target_file=None, **zipfile_args):
    """创建一个 zip 文件。

    :param list files: 要创建zip 的文件列表。
    :param int trim_arcname: 若提供这个值，则使用 ZipFile.write(filename, filename[trim_arcname:]) 进行调用。
    :returns: zip 文件的路径。
    :rtype: str

    """
    zipname = None
    azip = None
    if not target_file:
        azip = tempfile.NamedTemporaryFile(mode='wb', delete=False)
        zipname = azip.name
    else:
        azip = target_file
        zipname = target_file.name if hasattr(azip, 'read') else azip
    slog.info('Package %d files to "%s"'%(len(files), azip.name))
    fileNum = len(files)
    curFile = 0
    zipfile_args['mode'] = 'w'
    if not zipfile_args.get('compression'):
        zipfile_args['compression'] = zipfile.ZIP_DEFLATED
    with zipfile.ZipFile(azip, **zipfile_args) as zipf:
       for f in files:
           percent = round(curFile/fileNum*100)
           sys.stdout.write('\r%d%%'%(percent))
           sys.stdout.flush()
           zipf.write(f, f[trim_arcname:] if trim_arcname else None )
           curFile = curFile+1

       sys.stdout.write('\r100%\n')
       sys.stdout.flush()

    if hasattr(azip, 'close'):
        azip.close()
    return zipname
def create_zip ( files , trim_arcname = None , target_file = None , ** zipfile_args ) : zipname = None azip = None if not target_file : azip = tempfile . NamedTemporaryFile ( mode = 'wb' , delete = False ) zipname = azip . name else : azip = target_file zipname = target_file . name if hasattr ( azip , 'read' ) else azip slog . info ( 'Package %d files to "%s"' % ( len ( files ) , azip . name ) ) fileNum = len ( files ) curFile = 0 zipfile_args [ 'mode' ] = 'w' if not zipfile_args . get ( 'compression' ) : zipfile_args [ 'compression' ] = zipfile . ZIP_DEFLATED with zipfile . ZipFile ( azip , ** zipfile_args ) as zipf : for f in files : percent = round ( curFile / fileNum * 100 ) sys . stdout . write ( '\r%d%%' % ( percent ) ) sys . stdout . flush ( ) zipf . write ( f , f [ trim_arcname : ] if trim_arcname else None ) curFile = curFile + 1 sys . stdout . write ( '\r100%\n' ) sys . stdout . flush ( ) if hasattr ( azip , 'close' ) : azip . close ( ) return zipname
创建一个 zip 文件。

    :param list files: 要创建zip 的文件列表。
    :param int trim_arcname: 若提供这个值，则使用 ZipFile.write(filename, filename[trim_arcname:]) 进行调用。
    :returns: zip 文件的路径。
    :rtype: str