Python lzma 模块,compress() 实例源码

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

项目:tasker    作者:wavenator    | 项目源码 | 文件源码
def compress(
        data,
    ):
        compressed_object = lzma.compress(data)

        return compressed_object
项目:Kaggle_HomeDepot    作者:ChenglongChen    | 项目源码 | 文件源码
def _compression_dist(x, y, l_x=None, l_y=None):
    if x == y:
        return 0
    x_b = x.encode('utf-8')
    y_b = y.encode('utf-8')
    if l_x is None:
        l_x = len(lzma.compress(x_b))
        l_y = len(lzma.compress(y_b))
    l_xy = len(lzma.compress(x_b+y_b))
    l_yx = len(lzma.compress(y_b+x_b))
    dist = np_utils._try_divide(min(l_xy,l_yx)-min(l_x,l_y), max(l_x,l_y))
    return dist
项目:Charcoal    作者:somebody1234    | 项目源码 | 文件源码
def CompressBrotli(string):
    """
    CompressBrotli(string) -> str
    Returns without delimiters the given string compressed \
using Google's brotli compression method.

    """
    compressed = brotli.compress(string)
    number = 1
    for c in compressed:
        number = number * 256 + c
    result = ""
    while number:
        result = Codepage[number % 255] + result
        number //= 255
    return Codepage[BROTLI_ENCODING] + result
项目:Charcoal    作者:somebody1234    | 项目源码 | 文件源码
def CompressLZMA(string):
    """
    CompressBrotli(string) -> str
    Returns without delimiters the given string compressed \
using the lzstring compression method.

    """
    compressed = lzma.compress(
        string.encode("ascii"),
        format=lzma.FORMAT_RAW,
        filters=[{'id': lzma.FILTER_LZMA2, 'preset': 9 | lzma.PRESET_EXTREME}]
    )
    number = 1
    for c in compressed:
        number = number * 256 + c
    result = ""
    while number:
        result = Codepage[number % 255] + result
        number //= 255
    return Codepage[LZMA_ENCODING] + result
项目:TACTIC-Handler    作者:listyque    | 项目源码 | 文件源码
def bz2_pack(source):
    """
    Returns 'source' as a bzip2-compressed, self-extracting python script.

    .. note::

        This method uses up more space than the zip_pack method but it has the
        advantage in that the resulting .py file can still be imported into a
        python program.
    """
    import bz2, base64
    out = ""
    # Preserve shebangs (don't care about encodings for this)
    first_line = source.split('\n')[0]
    if analyze.shebang.match(first_line):
        if py3:
            if first_line.rstrip().endswith('python'): # Make it python3
                first_line = first_line.rstrip()
                first_line += '3' #!/usr/bin/env python3
        out = first_line + '\n'
    compressed_source = bz2.compress(source.encode('utf-8'))
    out += 'import bz2, base64\n'
    out += "exec(bz2.decompress(base64.b64decode('"
    out += base64.b64encode(compressed_source).decode('utf-8')
    out += "')))\n"
    return out
项目:TACTIC-Handler    作者:listyque    | 项目源码 | 文件源码
def gz_pack(source):
    """
    Returns 'source' as a gzip-compressed, self-extracting python script.

    .. note::

        This method uses up more space than the zip_pack method but it has the
        advantage in that the resulting .py file can still be imported into a
        python program.
    """
    import zlib, base64
    out = ""
    # Preserve shebangs (don't care about encodings for this)
    first_line = source.split('\n')[0]
    if analyze.shebang.match(first_line):
        if py3:
            if first_line.rstrip().endswith('python'): # Make it python3
                first_line = first_line.rstrip()
                first_line += '3' #!/usr/bin/env python3
        out = first_line + '\n'
    compressed_source = zlib.compress(source.encode('utf-8'))
    out += 'import zlib, base64\n'
    out += "exec(zlib.decompress(base64.b64decode('"
    out += base64.b64encode(compressed_source).decode('utf-8')
    out += "')))\n"
    return out
项目:TACTIC-Handler    作者:listyque    | 项目源码 | 文件源码
def lzma_pack(source):
    """
    Returns 'source' as a lzma-compressed, self-extracting python script.

    .. note::

        This method uses up more space than the zip_pack method but it has the
        advantage in that the resulting .py file can still be imported into a
        python program.
    """
    import lzma, base64
    out = ""
    # Preserve shebangs (don't care about encodings for this)
    first_line = source.split('\n')[0]
    if analyze.shebang.match(first_line):
        if py3:
            if first_line.rstrip().endswith('python'): # Make it python3
                first_line = first_line.rstrip()
                first_line += '3' #!/usr/bin/env python3
        out = first_line + '\n'
    compressed_source = lzma.compress(source.encode('utf-8'))
    out += 'import lzma, base64\n'
    out += "exec(lzma.decompress(base64.b64decode('"
    out += base64.b64encode(compressed_source).decode('utf-8')
    out += "')))\n"
    return out
项目:py2p    作者:p2p-today    | 项目源码 | 文件源码
def string(self):
        # type: (InternalMessage) -> bytes
        """Returns a :py:class:`bytes` representation of the message

        Raises:
            TypeError: See :py:func:`~py2p.base.InternalMessage._InternalMessage__non_len_string`
        """
        if not all((self.__id, self.__string, self.__full_string)):
            id_ = self.id
            ret = b''.join((id_, self.__non_len_string))
            compression_used = self.compression_used
            if compression_used:
                ret = compress(ret, compression_used)
            self.__full_string = b''.join((pack_value(4, len(ret)), ret))
        return self.__full_string
项目:amprolla    作者:parazyd    | 项目源码 | 文件源码
def rehash_release(_filelist, fdesc, rmstr):
    """
    Calculates checksums of a given filelist and writes them to the given
    file descriptor. Takes rmstr as the third argument, which is a string to
    remove from the path of the hashed file when writing it to a file.
    """
    info('Hashing checksums')
    for csum in checksums:
        fdesc.write('%s:\n' % csum['name'])
        for i in _filelist:
            if isfile(i):
                cont = open(i, 'rb').read()
                fdesc.write(' %s %8s %s\n' % (csum['f'](cont).hexdigest(),
                                              getsize(i),
                                              i.replace(rmstr+'/', '')))
            elif i.endswith('.xz') and isfile(i.replace('.xz', '.gz')):
                xzstr = lzma_comp(open(i.replace('.xz', '.gz'), 'rb').read())
                fdesc.write(' %s %8s %s\n' % (csum['f'](xzstr).hexdigest(),
                                              len(xzstr),
                                              i.replace(rmstr+'/', '')))
            elif not i.endswith('.gz') and isfile(i+'.gz'):
                uncomp = gzip_decomp(open(i+'.gz', 'rb').read())
                fdesc.write(' %s %8s %s\n' % (csum['f'](uncomp).hexdigest(),
                                              len(uncomp),
                                              i.replace(rmstr+'/', '')))
    return
项目:shellsploit-library    作者:riusksk    | 项目源码 | 文件源码
def gz_pack(source):
    """
    Returns 'source' as a gzip-compressed, self-extracting python script.

    .. note::

        This method uses up more space than the zip_pack method but it has the
        advantage in that the resulting .py file can still be imported into a
        python program.
    """
    import zlib
    import base64
    out = ""
    # Preserve shebangs (don't care about encodings for this)
    first_line = source.split('\n')[0]
    if analyze.shebang.match(first_line):
        if py3:
            if first_line.rstrip().endswith('python'):  # Make it python3
                first_line = first_line.rstrip()
                first_line += '3'  # !/usr/bin/env python3
        out = first_line + '\n'
    compressed_source = zlib.compress(source.encode('utf-8'))
    out += 'import zlib, base64\n'
    out += "exec(zlib.decompress(base64.b64decode('"
    out += base64.b64encode(compressed_source).decode('utf-8')
    out += "')))\n"
    return out
项目:shellsploit-library    作者:riusksk    | 项目源码 | 文件源码
def lzma_pack(source):
    """
    Returns 'source' as a lzma-compressed, self-extracting python script.

    .. note::

        This method uses up more space than the zip_pack method but it has the
        advantage in that the resulting .py file can still be imported into a
        python program.
    """
    import lzma
    import base64
    out = ""
    # Preserve shebangs (don't care about encodings for this)
    first_line = source.split('\n')[0]
    if analyze.shebang.match(first_line):
        if py3:
            if first_line.rstrip().endswith('python'):  # Make it python3
                first_line = first_line.rstrip()
                first_line += '3'  # !/usr/bin/env python3
        out = first_line + '\n'
    compressed_source = lzma.compress(source.encode('utf-8'))
    out += 'import lzma, base64\n'
    out += "exec(lzma.decompress(base64.b64decode('"
    out += base64.b64encode(compressed_source).decode('utf-8')
    out += "')))\n"
    return out
项目:django-asyncio-redis    作者:mackeyja92    | 项目源码 | 文件源码
def compress(self, data):
        if len(data) > self.min_length:
            return lzma.compress(data, preset=self.preset)
        return data
项目:zhihu-machine-learning-challenge-2017    作者:HouJP    | 项目源码 | 文件源码
def compression_dist(x, y, l_x=None, l_y=None):
        if x == y:
            return 0
        x_b = x.encode('utf-8')
        y_b = y.encode('utf-8')
        if l_x is None:
            l_x = len(lzma.compress(x_b))
            l_y = len(lzma.compress(y_b))
        l_xy = len(lzma.compress(x_b + y_b))
        l_yx = len(lzma.compress(y_b + x_b))
        dist = MathUtil.try_divide(min(l_xy, l_yx) - min(l_x, l_y), max(l_x, l_y))
        return dist
项目:kaggle-quora-solution-8th    作者:qqgeogor    | 项目源码 | 文件源码
def _compression_dist(x, y, l_x=None, l_y=None):
    if x == y:
        return 0
    x_b = x.encode('utf-8')
    y_b = y.encode('utf-8')
    if l_x is None:
        l_x = len(lzma.compress(x_b))
        l_y = len(lzma.compress(y_b))
    l_xy = len(lzma.compress(x_b+y_b))
    l_yx = len(lzma.compress(y_b+x_b))
    dist = np_utils._try_divide(min(l_xy,l_yx)-min(l_x,l_y), max(l_x,l_y))
    return dist
项目:SoS    作者:vatlab    | 项目源码 | 文件源码
def save(self, job_file):
        with open(job_file, 'wb') as jf:
            jf.write(f'SOSTASK1.2\n{" ".join(self.tags)}\n'.encode())
            # remove __builtins__ from sos_dict #835
            if 'CONFIG' in self.sos_dict and '__builtins__' in self.sos_dict['CONFIG']:
                self.sos_dict['CONFIG'].pop('__builtins__')
            try:
                jf.write(lzma.compress(pickle.dumps(self)))
            except Exception as e:
                env.logger.warning(e)
                raise
项目:ekko    作者:openstack    | 项目源码 | 文件源码
def compress(data):
        return lzma.compress(data)
项目:chrome-prerender    作者:bosondata    | 项目源码 | 文件源码
def set(self, key: str, payload: bytes, ttl: int = None, format: str = 'html') -> None:
        compressed = lzma.compress(payload)
        self._cache.set(key + format, compressed, expire=ttl)
项目:py2p    作者:p2p-today    | 项目源码 | 文件源码
def compress(msg, method):
    # type: (bytes, int) -> bytes
    """Shortcut method for compression

    Args:
        msg:    The message you wish to compress, the type required is
                    defined by the requested method
        method: The compression method you wish to use. Supported
                    (assuming installed):

                    - :py:data:`~py2p.flags.gzip`
                    - :py:data:`~py2p.flags.zlib`
                    - :py:data:`~py2p.flags.bz2`
                    - :py:data:`~py2p.flags.lzma`
                    - :py:data:`~py2p.flags.snappy`

    Returns:
        Defined by the compression method, but typically the bytes of the
        compressed message

    Warning:
        The types fed are dependent on which compression method you use.
        Best to assume most values are :py:class:`bytes` or
        :py:class:`bytearray`

    Raises:
        ValueError: if there is an unknown compression method, or a
            method-specific error
    """
    if method in (flags.gzip, flags.zlib):
        wbits = 15 + (16 * (method == flags.gzip))
        compressor = zlib.compressobj(zlib.Z_DEFAULT_COMPRESSION,
                                      zlib.DEFLATED, wbits)
        return compressor.compress(msg) + compressor.flush()
    elif method == flags.bz2:
        return bz2.compress(msg)
    elif method == flags.lzma:
        return lzma.compress(msg)
    elif method == flags.snappy:
        return snappy.compress(msg)
    else:  # pragma: no cover
        raise ValueError('Unknown compression method')
项目:arclib    作者:kirbyfan64    | 项目源码 | 文件源码
def test_incremental_decompress():
    basic_test_d(lzma.Decompressor(), compress)
项目:shellsploit-library    作者:riusksk    | 项目源码 | 文件源码
def bz2_pack(source):
    """
    Returns 'source' as a bzip2-compressed, self-extracting python script.

    .. note::

        This method uses up more space than the zip_pack method but it has the
        advantage in that the resulting .py file can still be imported into a
        python program.
    """
    import bz2
    import base64
    out = ""
    # Preserve shebangs (don't care about encodings for this)
    first_line = source.split('\n')[0]
    if analyze.shebang.match(first_line):
        if py3:
            if first_line.rstrip().endswith('python'):  # Make it python3
                first_line = first_line.rstrip()
                first_line += '3'  # !/usr/bin/env python3
        out = first_line + '\n'
    compressed_source = bz2.compress(source.encode('utf-8'))
    out += 'import bz2, base64\n'
    out += "exec(bz2.decompress(base64.b64decode('"
    out += base64.b64encode(compressed_source).decode('utf-8')
    out += "')))\n"
    return out