本文整理汇总了Python中brotli.compress方法的典型用法代码示例。如果您正苦于以下问题:Python brotli.compress方法的具体用法?Python brotli.compress怎么用?Python brotli.compress使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类brotli
的用法示例。
在下文中一共展示了brotli.compress方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: CompressBrotli
# 需要导入模块: import brotli [as 别名]
# 或者: from brotli import compress [as 别名]
def CompressBrotli(string):
"""
CompressBrotli(string) -> str
Returns without delimiters the given string compressed \
using Google's brotli compression method.
"""
compressed = brotli.compress(string.encode("ascii"))
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
示例2: CompressLZMA
# 需要导入模块: import brotli [as 别名]
# 或者: from brotli import compress [as 别名]
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
示例3: post_build
# 需要导入模块: import brotli [as 别名]
# 或者: from brotli import compress [as 别名]
def post_build(self, pkt, pay):
if not conf.contribs["http"]["auto_compression"]:
return pkt + pay
encodings = self._get_encodings()
# Compress
if "deflate" in encodings:
import zlib
pay = zlib.compress(pay)
elif "gzip" in encodings:
pay = gzip_compress(pay)
elif "compress" in encodings:
import lzw
pay = lzw.compress(pay)
elif "br" in encodings and is_brotli_available:
pay = brotli.compress(pay)
return pkt + pay
示例4: compress
# 需要导入模块: import brotli [as 别名]
# 或者: from brotli import compress [as 别名]
def compress(content, method='gzip', compress_level=None):
"""
Compresses file content.
Required:
content (bytes): The information to be compressed
method (str, default: 'gzip'): None or gzip
Raises:
NotImplementedError if an unsupported codec is specified.
compression.DecodeError if the encoder has an issue
Return: compressed content
"""
if method == True:
method = 'gzip' # backwards compatibility
method = (method or '').lower()
if method == '':
return content
elif method == 'gzip':
return gzip_compress(content, compresslevel=compress_level)
elif method == 'br':
return brotli_compress(content, quality=compress_level)
raise NotImplementedError(str(method) + ' is not currently supported. Supported Options: None, gzip')
示例5: test_streaming_compression
# 需要导入模块: import brotli [as 别名]
# 或者: from brotli import compress [as 别名]
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()
示例6: _calcFlavorDataOffsetsAndSize
# 需要导入模块: import brotli [as 别名]
# 或者: from brotli import compress [as 别名]
def _calcFlavorDataOffsetsAndSize(self, start):
"""Calculate offsets and lengths for any meta- and/or private data."""
offset = start
data = self.flavorData
if data.metaData:
self.metaOrigLength = len(data.metaData)
self.metaOffset = offset
self.compressedMetaData = brotli.compress(
data.metaData, mode=brotli.MODE_TEXT)
self.metaLength = len(self.compressedMetaData)
offset += self.metaLength
else:
self.metaOffset = self.metaLength = self.metaOrigLength = 0
self.compressedMetaData = b""
if data.privData:
# make sure private data is padded to 4-byte boundary
offset = (offset + 3) & ~3
self.privOffset = offset
self.privLength = len(data.privData)
offset += self.privLength
else:
self.privOffset = self.privLength = 0
return offset
示例7: post_dissect
# 需要导入模块: import brotli [as 别名]
# 或者: from brotli import compress [as 别名]
def post_dissect(self, s):
if not conf.contribs["http"]["auto_compression"]:
return s
encodings = self._get_encodings()
# Un-chunkify
if "chunked" in encodings:
data = b""
while s:
length, _, body = s.partition(b"\r\n")
try:
length = int(length, 16)
except ValueError:
# Not a valid chunk. Ignore
break
else:
load = body[:length]
if body[length:length + 2] != b"\r\n":
# Invalid chunk. Ignore
break
s = body[length + 2:]
data += load
if not s:
s = data
# Decompress
try:
if "deflate" in encodings:
import zlib
s = zlib.decompress(s)
elif "gzip" in encodings:
s = gzip_decompress(s)
elif "compress" in encodings:
import lzw
s = lzw.decompress(s)
elif "br" in encodings and is_brotli_available:
s = brotli.decompress(s)
except Exception:
# Cannot decompress - probably incomplete data
pass
return s
示例8: encode_image_url
# 需要导入模块: import brotli [as 别名]
# 或者: from brotli import compress [as 别名]
def encode_image_url(url, referer=None):
try:
text = json.dumps(validate_image_url(dict(url=url, referer=referer)))
data = brotli.compress(text.encode('utf-8'), quality=6)
return base64.urlsafe_b64encode(data).decode()
except (Invalid, json.JSONDecodeError, brotli.error, UnicodeEncodeError) as ex:
raise ImageUrlEncodeError(str(ex)) from ex
示例9: verify_and_get_compress_func
# 需要导入模块: import brotli [as 别名]
# 或者: from brotli import compress [as 别名]
def verify_and_get_compress_func(compression_type, compression_level=9):
# type: (str, int) -> Optional[Callable]
"""
Given a compression_type (bz2, zlib, lz4, zstandard), verify that compression works and return
the compress() function with compression level pre-applied.
@param compression_type: Compression type.
@type compression_type: str
@param compression_level: Compression level to use.
@ type: compression_level: int
@returns: The compress() function for the specified compression_type. None, if compression_type
is not supported or if underlying libs are not installed properly,
"""
if compression_type not in SUPPORTED_COMPRESSION_ALGORITHMS:
return None
try:
compress_func, decompress_func = get_compress_and_decompress_func(
compression_type, compression_level=compression_level
)
# Perform a sanity check that data compresses and that it can be decompressed
cdata = compress_func(COMPRESSION_TEST_STR)
if (
len(cdata) < len(COMPRESSION_TEST_STR)
and decompress_func(cdata) == COMPRESSION_TEST_STR
):
return compress_func
except Exception:
pass
return None
示例10: brotli_compress
# 需要导入模块: import brotli [as 别名]
# 或者: from brotli import compress [as 别名]
def brotli_compress(content, quality=None):
if quality is None:
# 5/6 are good balance between compression speed and compression rate
quality = 5
return brotli.compress(content, quality=quality)
示例11: test_roundtrip_compression_with_files
# 需要导入模块: import brotli [as 别名]
# 或者: from brotli import compress [as 别名]
def test_roundtrip_compression_with_files(simple_compressed_file):
"""
Roundtripping data through the compressor works correctly.
"""
with open(simple_compressed_file[0], 'rb') as f:
uncompressed_data = f.read()
assert brotli.decompress(
brotli.compress(uncompressed_data)
) == uncompressed_data
示例12: test_streaming_compression_flush
# 需要导入模块: import brotli [as 别名]
# 或者: from brotli import compress [as 别名]
def test_streaming_compression_flush(one_compressed_file,
chunk_size,
mode,
quality,
lgwin,
lgblock):
"""
Confirm that the streaming compressor works as expected, including flushes
after each chunk.
"""
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.flush())
compressed_chunks.append(c.finish())
decompressed = brotli.decompress(b''.join(compressed_chunks))
with open(one_compressed_file, 'rb') as f:
assert decompressed == f.read()
示例13: test_compressed_data_roundtrips
# 需要导入模块: import brotli [as 别名]
# 或者: from brotli import compress [as 别名]
def test_compressed_data_roundtrips(s):
assert brotli.decompress(brotli.compress(s)) == s
示例14: test_compressed_data_with_dictionaries
# 需要导入模块: import brotli [as 别名]
# 或者: from brotli import compress [as 别名]
def test_compressed_data_with_dictionaries(s, dictionary):
d = brotli.Decompressor(dictionary)
compressed = brotli.compress(s, dictionary=dictionary)
uncompressed = d.decompress(compressed)
assert uncompressed == s
示例15: setUpClass
# 需要导入模块: import brotli [as 别名]
# 或者: from brotli import compress [as 别名]
def setUpClass(cls):
assert os.path.exists(METADATA)
with open(METADATA, 'rb') as f:
cls.xml_metadata = f.read()
cls.compressed_metadata = brotli.compress(cls.xml_metadata, mode=brotli.MODE_TEXT)
# make random byte strings; font data must be 4-byte aligned
cls.fontdata = bytes(bytearray(random.sample(range(0, 256), 80)))
cls.privData = bytes(bytearray(random.sample(range(0, 256), 20)))