Beispiel #1
0
 def __init__(self):
     if Zstd is None:
         raise UnsupportedCompressionMethodError
     self._buf = BufferedRW()
     ctx = Zstd.ZstdCompressor()  # type: ignore
     self._compressor = ctx.stream_writer(self._buf)
     self.flushed = False
Beispiel #2
0
 def __init__(self, properties):
     if Zstd is None or len(properties) not in [
             3, 5
     ] or (properties[0], properties[1], 0) > Zstd.ZSTD_VERSION:
         raise UnsupportedCompressionMethodError
     self._buf = BufferedRW()
     ctx = Zstd.ZstdDecompressor()  # type: ignore
     self._decompressor = ctx.stream_writer(self._buf)
class PpmdCompressor(ISevenZipCompressor):
    """Compress with PPMd compression algorithm"""
    def __init__(self, level: int, mem: int, blocksize: Optional[int] = None):
        self._buf = BufferedRW(blocksize)
        self.encoder = Ppmd.Ppmd7Encoder(self._buf, level, mem)  # type: ignore

    def compress(self, data: Union[bytes, bytearray, memoryview]) -> bytes:
        self.encoder.encode(data)
        return self._buf.read()

    def flush(self):
        self.encoder.flush()
        return self._buf.read()
Beispiel #4
0
class PpmdCompressor(ISevenZipCompressor):
    def __init__(self, level: int, mem: int):
        if Ppmd is None:
            raise UnsupportedCompressionMethodError
        self._buf = BufferedRW()
        self.encoder = Ppmd.Ppmd7Encoder(self._buf, level, mem)  # type: ignore

    def compress(self, data: Union[bytes, bytearray, memoryview]) -> bytes:
        self.encoder.encode(data)
        return self._buf.read()

    def flush(self):
        self.encoder.flush()
        return self._buf.read()
Beispiel #5
0
 def __init__(self, properties: bytes):
     if Ppmd is None:
         raise UnsupportedCompressionMethodError
     if not isinstance(properties, bytes):
         raise UnsupportedCompressionMethodError
     if len(properties) == 5:
         level, mem = struct.unpack("<BL", properties)
     elif len(properties) == 7:
         level, mem, _, _ = struct.unpack("<BLBB", properties)
     else:
         raise UnsupportedCompressionMethodError
     self._buf = BufferedRW()
     self.decoder = None
     self.level = level
     self.mem = mem
     self.initialized = False
Beispiel #6
0
class ZstdDecompressor(ISevenZipDecompressor):

    def __init__(self, properties):
        if Zstd is None or len(properties) not in [3, 5] or (properties[0], properties[1], 0) > Zstd.ZSTD_VERSION:
            raise UnsupportedCompressionMethodError
        self._buf = BufferedRW()
        ctx = Zstd.ZstdDecompressor()  # type: ignore
        self._decompressor = ctx.stream_writer(self._buf)

    def decompress(self, data: Union[bytes, bytearray, memoryview], max_length: int = -1) -> bytes:
        self._decompressor.write(data)
        if max_length > 0:
            result = self._buf.read(max_length)
        else:
            result = self._buf.read()
        return result
class PpmdDecompressor(ISevenZipDecompressor):
    """Decompress PPMd compressed data"""
    def __init__(self, properties: bytes, blocksize: Optional[int] = None):
        if not isinstance(properties, bytes):
            raise UnsupportedCompressionMethodError
        if len(properties) == 5:
            level, mem = struct.unpack("<BL", properties)
        elif len(properties) == 7:
            level, mem, _, _ = struct.unpack("<BLBB", properties)
        else:
            raise UnsupportedCompressionMethodError
        if blocksize:
            self.block_size = blocksize
        else:
            self.block_size = get_default_blocksize()
        self._buf = BufferedRW(self.block_size)
        self._buf = BufferedRW()
        self.decoder = None
        self.level = level
        self.mem = mem
        self.initialized = False

    def _init2(self):
        self.decoder = Ppmd.Ppmd7Decoder(self._buf, self.level,
                                         self.mem)  # type: ignore
        self.initialized = True

    def decompress(self,
                   data: Union[bytes, bytearray, memoryview],
                   max_length=-1) -> bytes:
        self._buf.write(data)
        if not self.initialized:
            if len(self._buf) <= 4:
                return b""
            self._init2()
        assert self.decoder is not None
        if max_length <= 0:
            return self.decoder.decode(1)
        if len(data) == 0:
            return self.decoder.decode(max_length)
        #
        size = min(self.block_size, max_length)
        res = bytearray()
        while len(self._buf) > 0 and len(res) < size:
            res += self.decoder.decode(1)
        return bytes(res)
 def __init__(self, properties: bytes, blocksize: Optional[int] = None):
     if not isinstance(properties, bytes):
         raise UnsupportedCompressionMethodError
     if len(properties) == 5:
         level, mem = struct.unpack("<BL", properties)
     elif len(properties) == 7:
         level, mem, _, _ = struct.unpack("<BLBB", properties)
     else:
         raise UnsupportedCompressionMethodError
     if blocksize:
         self.block_size = blocksize
     else:
         self.block_size = get_default_blocksize()
     self._buf = BufferedRW(self.block_size)
     self._buf = BufferedRW()
     self.decoder = None
     self.level = level
     self.mem = mem
     self.initialized = False
Beispiel #9
0
class ZstdCompressor():
    def __init__(self, level):
        self._buf = BufferedRW()
        ctx = zstandard.ZstdCompressor(level)  # type: ignore
        self._compressor = ctx.stream_writer(self._buf)
        self.flushed = False

    def compress(self, data: Union[bytes, bytearray, memoryview]) -> bytes:
        self._compressor.write(data)
        result = self._buf.read()
        return result

    def flush(self):
        if self.flushed:
            return None
        self._compressor.flush(zstandard.FLUSH_FRAME)
        self.flushed = True
        result = self._buf.read()
        return result
Beispiel #10
0
class ZstdCompressor(ISevenZipCompressor):
    def __init__(self):
        if Zstd is None:
            raise UnsupportedCompressionMethodError
        self._buf = BufferedRW()
        ctx = Zstd.ZstdCompressor()  # type: ignore
        self._compressor = ctx.stream_writer(self._buf)
        self.flushed = False

    def compress(self, data: Union[bytes, bytearray, memoryview]) -> bytes:
        self._compressor.write(data)
        result = self._buf.read()
        return result

    def flush(self):
        if self.flushed:
            return None
        self._compressor.flush(Zstd.FLUSH_FRAME)
        self.flushed = True
        result = self._buf.read()
        return result
Beispiel #11
0
 def __init__(self, level: int, mem: int):
     if Ppmd is None:
         raise UnsupportedCompressionMethodError
     self._buf = BufferedRW()
     self.encoder = Ppmd.Ppmd7Encoder(self._buf, level, mem)  # type: ignore
 def __init__(self, level: int, mem: int, blocksize: Optional[int] = None):
     self._buf = BufferedRW(blocksize)
     self.encoder = Ppmd.Ppmd7Encoder(self._buf, level, mem)  # type: ignore
Beispiel #13
0
 def __init__(self, level):
     self._buf = BufferedRW()
     ctx = zstandard.ZstdCompressor(level)  # type: ignore
     self._compressor = ctx.stream_writer(self._buf)
     self.flushed = False