Python gzip 模块,html() 实例源码

我们从Python开源项目中,提取了以下3个代码示例,用于说明如何使用gzip.html()

项目:skaff    作者:jhxie    | 项目源码 | 文件源码
def _manuals_copy(directory, log=None, *manuals):
    """
    Copies the gzipped manual page(s) in 'manuals' to the subdirectory of
    'directory' that ends with an extra manual section number if it exists;
    for example, if one of the basename of manual in 'manuals' is named 'git.1'
    and a subdirectory '/usr/share/man/man1/' exists, it will be installed to
    that subdirectory instead; otherwise falls back to '/usr/share/man/'.

    Also invokes the 'write' method of 'log' to append the file names of
    gzipped manual page(s) copied if 'log' is not None.
    """
    # Based on example from
    # https://docs.python.org/3/library/gzip.html
    for manual in manuals:
        if not manual_check(manual):
            raise TypeError(("The manual page {0} ".format(manual) +
                             "is not in unix 'manpage' format"))
        if not os.path.isfile(manual):
            raise FileNotFoundError(("The manual page {0} ".format(manual) +
                                     "does not exist"))

        # Test whether there exists extra manual subdirectory ends with
        # manual section number; for example,
        # '/usr/share/man/man1/'
        # if so, use this subdirectory instead; otherwise fall back to
        # '/usr/share/man/'
        path_postfix = os.path.splitext(manual)[-1][-1]
        man_subdir = directory + "man" + path_postfix + os.sep

        if os.path.isdir(man_subdir) and os.access(man_subdir, os.W_OK):
            target_manpage = man_subdir + os.path.basename(manual) + ".gz"
        else:
            target_manpage = directory + os.path.basename(manual) + ".gz"

        with open(manual, "rb") as input_manpage:
            with gzip.open(target_manpage, "wb") as output_manpage:
                shutil.copyfileobj(input_manpage, output_manpage)
        if log:
            log.write(target_manpage)
项目:autosub-bootstrapbill    作者:BenjV    | 项目源码 | 文件源码
def compress(body, compress_level):
    """Compress 'body' at the given compress_level."""
    import zlib

    # See http://www.gzip.org/zlib/rfc-gzip.html
    yield ntob('\x1f\x8b')       # ID1 and ID2: gzip marker
    yield ntob('\x08')           # CM: compression method
    yield ntob('\x00')           # FLG: none set
    # MTIME: 4 bytes
    yield struct.pack('<L', int(time.time()) & int('FFFFFFFF', 16))
    yield ntob('\x02')           # XFL: max compression, slowest algo
    yield ntob('\xff')           # OS: unknown

    crc = zlib.crc32(ntob(''))
    size = 0
    zobj = zlib.compressobj(compress_level,
                            zlib.DEFLATED, -zlib.MAX_WBITS,
                            zlib.DEF_MEM_LEVEL, 0)
    for line in body:
        size += len(line)
        crc = zlib.crc32(line, crc)
        yield zobj.compress(line)
    yield zobj.flush()

    # CRC32: 4 bytes
    yield struct.pack('<L', crc & int('FFFFFFFF', 16))
    # ISIZE: 4 bytes
    yield struct.pack('<L', size & int('FFFFFFFF', 16))
项目:watcher    作者:nosmokingbandit    | 项目源码 | 文件源码
def compress(body, compress_level):
    """Compress 'body' at the given compress_level."""
    import zlib

    # See http://www.gzip.org/zlib/rfc-gzip.html
    yield ntob('\x1f\x8b')       # ID1 and ID2: gzip marker
    yield ntob('\x08')           # CM: compression method
    yield ntob('\x00')           # FLG: none set
    # MTIME: 4 bytes
    yield struct.pack("<L", int(time.time()) & int('FFFFFFFF', 16))
    yield ntob('\x02')           # XFL: max compression, slowest algo
    yield ntob('\xff')           # OS: unknown

    crc = zlib.crc32(ntob(""))
    size = 0
    zobj = zlib.compressobj(compress_level,
                            zlib.DEFLATED, -zlib.MAX_WBITS,
                            zlib.DEF_MEM_LEVEL, 0)
    for line in body:
        size += len(line)
        crc = zlib.crc32(line, crc)
        yield zobj.compress(line)
    yield zobj.flush()

    # CRC32: 4 bytes
    yield struct.pack("<L", crc & int('FFFFFFFF', 16))
    # ISIZE: 4 bytes
    yield struct.pack("<L", size & int('FFFFFFFF', 16))