Python lzma.FORMAT_RAW Examples

The following are 13 code examples of lzma.FORMAT_RAW(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module lzma , or try the search function .
Example #1
Source File: compression.py    From Charcoal with MIT License 6 votes vote down vote up
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 
Example #2
Source File: compression.py    From Charcoal with MIT License 6 votes vote down vote up
def DecompressLZMA(string):
    """
    DecompressBrotli(string) -> str
    Returns the original form of the given string compressed \
using Google's brotli compression method., passed without delimiters.

    """
    number = 0
    for character in string:
        ordinal = OrdinalLookup.get(character, ord(character))
        number = number * 255 + ordinal - (ordinal > gap)
    compressed = []
    while number > 1:
        compressed = [number % 256] + compressed
        number //= 256
    return lzma.decompress(
        bytes(compressed),
        format=lzma.FORMAT_RAW,
        filters=[{'id': lzma.FILTER_LZMA2, 'preset': 9 | lzma.PRESET_EXTREME}]
    ).decode("ascii") 
Example #3
Source File: assetbundle.py    From UnityPack with MIT License 6 votes vote down vote up
def decompress(self, buf):
		if not self.compressed:
			return buf
		ty = self.compression_type
		if ty == CompressionType.LZMA:
			props, dict_size = struct.unpack("<BI", buf.read(5))
			lc = props % 9
			props = int(props / 9)
			pb = int(props / 5)
			lp = props % 5
			dec = lzma.LZMADecompressor(format=lzma.FORMAT_RAW, filters=[{
				"id": lzma.FILTER_LZMA1,
				"dict_size": dict_size,
				"lc": lc,
				"lp": lp,
				"pb": pb,
			}])
			res = dec.decompress(buf.read())
			return BytesIO(res)
		if ty in (CompressionType.LZ4, CompressionType.LZ4HC):
			res = lz4_decompress(buf.read(self.compressed_size), self.uncompressed_size)
			return BytesIO(res)
		raise NotImplementedError("Unimplemented compression method: %r" % (ty)) 
Example #4
Source File: compressor.py    From py7zr with GNU Lesser General Public License v2.1 6 votes vote down vote up
def _set_native_compressors_coders(self, filters):
        self.cchain.add_filter(lzma.LZMACompressor(format=lzma.FORMAT_RAW, filters=filters))
        for filter in filters:
            self.coders.insert(0, SupportedMethods.get_coder(filter)) 
Example #5
Source File: fileform.py    From nrs with GNU General Public License v3.0 5 votes vote down vote up
def _lzma(f, size):
    import lzma
    data = f.read()
    props = lzma._decode_filter_properties(lzma.FILTER_LZMA1, data[0:5])
    return lzma.decompress(data[5:], lzma.FORMAT_RAW, filters=[props]) 
Example #6
Source File: slob.py    From slob with GNU General Public License v3.0 5 votes vote down vote up
def init_compressions():
    ident = lambda x: x
    compressions = {'': Compression(ident, ident)}
    for name in ('bz2', 'zlib'):
        try:
            m = __import__(name)
        except ImportError:
            warnings.warn('%s is not available' % name)
        else:
            compressions[name] = Compression(
                lambda x: m.compress(x, 9), m.decompress)

    try:
        import lzma
    except ImportError:
        warnings.warn('lzma is not available')
    else:
        filters = [{'id': lzma.FILTER_LZMA2}]
        compress = lambda s: lzma.compress(s,
                                           format=lzma.FORMAT_RAW,
                                           filters=filters)
        decompress = lambda s: lzma.decompress(s,
                                               format=lzma.FORMAT_RAW,
                                               filters=filters)
        compressions['lzma2'] = Compression(compress, decompress)
    return compressions 
Example #7
Source File: llcutils.py    From xmitgcm with MIT License 5 votes vote down vote up
def write_masks_to_zarr(ds_mask, output_path):
    import lzma
    lzma_filters = [dict(id=lzma.FILTER_DELTA, dist=1),
                    dict(id=lzma.FILTER_LZMA2, preset=1)]
    from numcodecs import LZMA
    compressor = LZMA(filters=lzma_filters, format=lzma.FORMAT_RAW)
    encoding = {vname: {'compressor': compressor} for vname in ds_mask.data_vars}
    return ds_mask.to_zarr(output_path, encoding=encoding) 
Example #8
Source File: compressor.py    From py7zr with GNU Lesser General Public License v2.1 5 votes vote down vote up
def get_lzma_decompressor(coders: List[Dict[str, Any]]):
    filters = []  # type: List[Dict[str, Any]]
    for coder in coders:
        if coder['numinstreams'] != 1 or coder['numoutstreams'] != 1:
            raise UnsupportedCompressionMethodError('Only a simple compression method is currently supported.')
        if not SupportedMethods.is_native_coder(coder):
            raise UnsupportedCompressionMethodError
        properties = coder.get('properties', None)
        filter_id = SupportedMethods.get_filter_id(coder)
        if properties is not None:
            filters[:0] = [lzma._decode_filter_properties(filter_id, properties)]  # type: ignore
        else:
            filters[:0] = [{'id': filter_id}]
    return lzma.LZMADecompressor(format=lzma.FORMAT_RAW, filters=filters) 
Example #9
Source File: test_extract.py    From py7zr with GNU Lesser General Public License v2.1 5 votes vote down vote up
def test_lzma_raw_decompressor_lzmabcj():
    # two files are compress same source by different methods
    indata = []
    with testdata_path.joinpath('lzma_bcj_1.7z').open('rb') as rawin:
        rawin.seek(32)
        indata.append(rawin.read(11327))
    with testdata_path.joinpath('lzma_bcj_2.7z').open('rb') as rawin:
        rawin.seek(32)
        indata.append(rawin.read(11334))
    filters1 = []
    filters1.append({'id': lzma.FILTER_X86})
    filters1.append(lzma._decode_filter_properties(lzma.FILTER_LZMA1, b']\x00\x00\x01\x00'))
    filters2 = []
    filters2.append({'id': lzma.FILTER_X86})
    filters2.append(lzma._decode_filter_properties(lzma.FILTER_LZMA2, b'\x0b'))
    decompressor1 = lzma.LZMADecompressor(format=lzma.FORMAT_RAW, filters=filters1)
    lzmabcj_out = decompressor1.decompress(indata[0])
    decompressor3 = lzma.LZMADecompressor(format=lzma.FORMAT_RAW, filters=filters2)
    lzma2bcj_out = decompressor3.decompress(indata[1])
    decompressor4 = lzma.LZMADecompressor(format=lzma.FORMAT_RAW, filters=filters1[1:])
    lzma_out = decompressor4.decompress(indata[0])
    decompressor5 = lzma.LZMADecompressor(format=lzma.FORMAT_RAW, filters=filters2[1:])
    lzma2_out = decompressor5.decompress(indata[1])
    #
    # filters1 pipeline:   indata[0] --> LZMA1 --> FILTER_X86 --> lzmabcj_out
    #                                     +----> lzma_out
    # filters2 pipeline:   indata[1] --> LZMA2 --> FILTER_X86 --> lzma2bcj_out
    #                                     +----> lzma2_out
    #
    # lzma_out and lzma2_out are same.
    #
    # lzmabcj_out and lzma2bcj_out should be same
    # but lzmabcj_out lacks last 4 bytes by python lzma/liblzma bug.
    #
    uncompress_size = 12800
    assert lzma_out == lzma2_out
    assert len(lzma2bcj_out) == uncompress_size
    assert len(lzmabcj_out) == uncompress_size 
Example #10
Source File: test_unit.py    From py7zr with GNU Lesser General Public License v2.1 5 votes vote down vote up
def test_lzma_lzma2bcj_compressor():
    filters = [{'id': 4}, {'id': 33, 'dict_size': 16777216}]
    assert lzma.LZMADecompressor(format=lzma.FORMAT_RAW, filters=filters) is not None 
Example #11
Source File: test_unit.py    From py7zr with GNU Lesser General Public License v2.1 5 votes vote down vote up
def test_lzmadecompressor_lzmabcj():
    indata = b'This file is located in the root.'
    compressor = lzma.LZMACompressor(format=lzma.FORMAT_RAW,
                                     filters=[{'id': lzma.FILTER_X86}, {'id': lzma.FILTER_LZMA1}])
    compressed = compressor.compress(indata)
    compressed += compressor.flush()
    decompressor = lzma.LZMADecompressor(format=lzma.FORMAT_RAW,
                                         filters=[{'id': lzma.FILTER_X86}, {'id': lzma.FILTER_LZMA1}])
    outdata = decompressor.decompress(data=compressed)
    assert outdata == indata 
Example #12
Source File: zip.py    From aptsources-cleanup with MIT License 5 votes vote down vote up
def _init(self):
			props = lzma._encode_filter_properties(self.compress_options)
			self._comp = lzma.LZMACompressor(
				lzma.FORMAT_RAW,
				filters=(
					lzma._decode_filter_properties(self.compress_options["id"], props),))
			return struct.pack("<BBH", 9, 4, len(props)) + props 
Example #13
Source File: test_unit.py    From py7zr with GNU Lesser General Public License v2.1 4 votes vote down vote up
def test_lzma_lzma2_compressor():
    filters = [{'id': 33, 'dict_size': 16777216}]
    assert lzma.LZMADecompressor(format=lzma.FORMAT_RAW, filters=filters) is not None