def save(self, filename, content): filename = super(BrotliCompressorFileStorage, self).save(filename, content) orig_path = self.path(filename) compressed_path = '%s.br' % orig_path import brotli br_compressor = brotli.Compressor() with open(orig_path, 'rb') as f_in, open(compressed_path, 'wb') as f_out: for f_in_data in iter(lambda: f_in.read(self.chunk_size), b''): compressed_data = br_compressor.process(f_in_data) if not compressed_data: compressed_data = br_compressor.flush() f_out.write(compressed_data) f_out.write(br_compressor.finish()) # Ensure the file timestamps match. # os.stat() returns nanosecond resolution on Linux, but os.utime() # only sets microsecond resolution. Set times on both files to # ensure they are equal. stamp = time.time() os.utime(orig_path, (stamp, stamp)) os.utime(compressed_path, (stamp, stamp)) return filename
def __init__(self, start_response, mime_types, quality, etag_alter): self._start_response = start_response self._mime_types = mime_types self._etag_alter = etag_alter self._compress = brotli.Compressor(quality=quality) self.brotli_ok = False
def _brotli_compress(raw_str, complevel=11, lgwin=24, lgblock=24): zipper = brotli.Compressor(mode=brotli.MODE_GENERIC, quality=complevel, lgwin=lgwin, lgblock=lgblock) compressed = zipper.process(raw_str) compressed += zipper.finish() return compressed
def main(self): rkpihdr = { 'sampleformat': self._SAMPLEFORMATS.index(self.args.pcm_sampleformat) + 1, 'compression': self._COMPRESSORS.index(self.args.rkpi_compression), 'bytedepth': self.args.pcm_bytedepth, 'samplerate': self.args.pcm_samplerate, 'channellayout': self.args.pcm_channellayout, 'endianness': int(self.args.pcm_endianness), 'channels': self.args.pcm_channels } # override bytedepth if the sampleformat forces so. if self.args.pcm_sampleformat in ('adpcm', 'a-law', 'mu-law'): rkpihdr['bytedepth'] = 1 # write out the header to file, doesn’t care about the # raw data in the file as there’s no size info. self.args.file_out.write(self._encode_rkpi_hdr(rkpihdr)) if self.args.rkpi_compression == 'none': if copyfile(self.args.file_in, self.args.file_out) == 0: stderr.write("Can’t do IO.") else: _COMPDS = partial(maprange, self.args.rkpi_compression_density, 100j) data_copied = 0 # how many bytes were copied compression. # Zstandard compressor. if rkpihdr['compression'] == 0b01: data_copied = copyfilemap( self.args.file_in, self.args.file_out, zstandard.ZstdCompressor(level=_COMPDS(1 + 22j)).compress) # Brotli compressor. if rkpihdr['compression'] == 0b10: comp = brotli.Compressor(quality=_COMPDS(11j), lgwin=24) data_copied = copyfilemap(self.args.file_in, self.args.file_out, comp.compress) data_copied += self.args.file_out.write(comp.finish()) # LZMA compressor. elif rkpihdr['compression'] == 0b11: data_copied = copyfile(self.args.file_in, lzma.LZMAFile(self.args.file_out, 'w')) if data_copied == 0: stderr.write("Compressor failed or can’t do IO.")
def compress_file(file): with open(file, encoding="utf8") as f: compressor = brotli.Compressor(mode=brotli.BrotliEncoderMode.TEXT) with tempfile.NamedTemporaryFile(prefix=(os.path.basename(file) + "."), suffix=".br", delete=False) as l: for line in f: data = line.encode() data = compressor.compress(data) l.write(compressor.flush()) l.write(compressor.finish()) return l.name
def test_streaming_compression(one_compressed_file, chunk_size, mode, quality, lgwin, lgblock): """ Confirm that the streaming compressor works as expected. """ compressed_chunks = [] c = brotli.Compressor(mode=mode, quality=quality, lgwin=lgwin, lgblock=lgblock) with open(one_compressed_file, 'rb') as f: while True: next_data = f.read(chunk_size) if not next_data: break compressed_chunks.append(c.compress(next_data)) compressed_chunks.append(c.finish()) decompressed = brotli.decompress(b''.join(compressed_chunks)) with open(one_compressed_file, 'rb') as f: assert decompressed == f.read()
def setUp(self): self.compressor = brotli.Compressor(quality=11)
def __init__(self, level): self._compressor = brotli.Compressor(quality=level)
import brotli with open('ipsum.raw') as f: decoded = f.read() with open('ipsum.brotli') as f: c = brotli.Compressor() c.process(decoded) encoded = c.finish() f.write(encoded)
def __init__(self): self.compressor = brotli.Compressor()
def test_bad_compressor_parameters(params, exception_cls): with pytest.raises(exception_cls): brotli.Compressor(**params)