本文整理汇总了Python中zlib.compressobj方法的典型用法代码示例。如果您正苦于以下问题:Python zlib.compressobj方法的具体用法?Python zlib.compressobj怎么用?Python zlib.compressobj使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类zlib
的用法示例。
在下文中一共展示了zlib.compressobj方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: gzip_app_iter
# 需要导入模块: import zlib [as 别名]
# 或者: from zlib import compressobj [as 别名]
def gzip_app_iter(app_iter):
size = 0
crc = zlib.crc32(b"") & 0xffffffff
compress = zlib.compressobj(9, zlib.DEFLATED, -zlib.MAX_WBITS,
zlib.DEF_MEM_LEVEL, 0)
yield _gzip_header
for item in app_iter:
size += len(item)
crc = zlib.crc32(item, crc) & 0xffffffff
# The compress function may return zero length bytes if the input is
# small enough; it buffers the input for the next iteration or for a
# flush.
result = compress.compress(item)
if result:
yield result
# Similarly, flush may also not yield a value.
result = compress.flush()
if result:
yield result
yield struct.pack("<2L", crc, size & 0xffffffff)
示例2: get_compression_options
# 需要导入模块: import zlib [as 别名]
# 或者: from zlib import compressobj [as 别名]
def get_compression_options(self) -> Optional[Dict[str, Any]]:
"""Override to return compression options for the connection.
If this method returns None (the default), compression will
be disabled. If it returns a dict (even an empty one), it
will be enabled. The contents of the dict may be used to
control the following compression options:
``compression_level`` specifies the compression level.
``mem_level`` specifies the amount of memory used for the internal compression state.
These parameters are documented in details here:
https://docs.python.org/3.6/library/zlib.html#zlib.compressobj
.. versionadded:: 4.1
.. versionchanged:: 4.5
Added ``compression_level`` and ``mem_level``.
"""
# TODO: Add wbits option.
return None
示例3: __init__
# 需要导入模块: import zlib [as 别名]
# 或者: from zlib import compressobj [as 别名]
def __init__(self, id, len, file, extra=None):
"""id: object id of stream; len: an unused Reference object for the
length of the stream, or None (to use a memory buffer); file:
a PdfFile; extra: a dictionary of extra key-value pairs to
include in the stream header """
self.id = id # object id
self.len = len # id of length object
self.pdfFile = file
self.file = file.fh # file to which the stream is written
self.compressobj = None # compression object
if extra is None: self.extra = dict()
else: self.extra = extra
self.pdfFile.recordXref(self.id)
if rcParams['pdf.compression']:
self.compressobj = zlib.compressobj(rcParams['pdf.compression'])
if self.len is None:
self.file = BytesIO()
else:
self._writeHeader()
self.pos = self.file.tell()
示例4: _newKeys
# 需要导入模块: import zlib [as 别名]
# 或者: from zlib import compressobj [as 别名]
def _newKeys(self):
"""
Called back by a subclass once a I{MSG_NEWKEYS} message has been
received. This indicates key exchange has completed and new encryption
and compression parameters should be adopted. Any messages which were
queued during key exchange will also be flushed.
"""
log.msg('NEW KEYS')
self.currentEncryptions = self.nextEncryptions
if self.outgoingCompressionType == b'zlib':
self.outgoingCompression = zlib.compressobj(6)
if self.incomingCompressionType == b'zlib':
self.incomingCompression = zlib.decompressobj()
self._keyExchangeState = self._KEY_EXCHANGE_NONE
messages = self._blockedByKeyExchange
self._blockedByKeyExchange = None
for (messageType, payload) in messages:
self.sendPacket(messageType, payload)
示例5: get_compression_options
# 需要导入模块: import zlib [as 别名]
# 或者: from zlib import compressobj [as 别名]
def get_compression_options(self):
"""Override to return compression options for the connection.
If this method returns None (the default), compression will
be disabled. If it returns a dict (even an empty one), it
will be enabled. The contents of the dict may be used to
control the following compression options:
``compression_level`` specifies the compression level.
``mem_level`` specifies the amount of memory used for the internal compression state.
These parameters are documented in details here:
https://docs.python.org/3.6/library/zlib.html#zlib.compressobj
.. versionadded:: 4.1
.. versionchanged:: 4.5
Added ``compression_level`` and ``mem_level``.
"""
# TODO: Add wbits option.
return None
示例6: enc
# 需要导入模块: import zlib [as 别名]
# 或者: from zlib import compressobj [as 别名]
def enc(self, data, final):
buf = []
if not self.writeheader:
h = header.new()
h.mtime = int(time.time())
h.fname = self.fname
buf.append(header.tobytes(h))
self.compobj = zlib.compressobj(self.level, zlib.DEFLATED, -zlib.MAX_WBITS, zlib.DEF_MEM_LEVEL)
self.writeheader = True
buf.append(self.compobj.compress(data))
self.crc = zlib.crc32(data, self.crc) & 0xffffffff
self.size += len(data)
if final:
buf.append(self.compobj.flush())
t = tail.new()
t.crc32 = self.crc
t.isize = self.size
buf.append(tail.tobytes(t))
return b''.join(buf)
示例7: __init__
# 需要导入模块: import zlib [as 别名]
# 或者: from zlib import compressobj [as 别名]
def __init__(self, id, len, file, extra=None):
"""id: object id of stream; len: an unused Reference object for the
length of the stream, or None (to use a memory buffer); file:
a PdfFile; extra: a dictionary of extra key-value pairs to
include in the stream header """
self.id = id # object id
self.len = len # id of length object
self.pdfFile = file
self.file = file.fh # file to which the stream is written
self.compressobj = None # compression object
if extra is None:
self.extra = dict()
else:
self.extra = extra
self.pdfFile.recordXref(self.id)
if rcParams['pdf.compression']:
self.compressobj = zlib.compressobj(rcParams['pdf.compression'])
if self.len is None:
self.file = BytesIO()
else:
self._writeHeader()
self.pos = self.file.tell()
示例8: handle
# 需要导入模块: import zlib [as 别名]
# 或者: from zlib import compressobj [as 别名]
def handle(self, connection):
exported = self.createPlaylist(connection.headers['Host'], connection.reqtype, query_get(connection.query, 'fmt')).encode('utf-8')
connection.send_response(200)
connection.send_header('Content-Type', 'audio/mpegurl; charset=utf-8')
connection.send_header('Access-Control-Allow-Origin', '*')
try:
h = connection.headers.get('Accept-Encoding').split(',')[0]
compress_method = { 'zlib': zlib.compressobj(9, zlib.DEFLATED, zlib.MAX_WBITS),
'deflate': zlib.compressobj(9, zlib.DEFLATED, -zlib.MAX_WBITS),
'gzip': zlib.compressobj(9, zlib.DEFLATED, zlib.MAX_WBITS | 16) }
exported = compress_method[h].compress(exported) + compress_method[h].flush()
connection.send_header('Content-Encoding', h)
except: pass
connection.send_header('Content-Length', len(exported))
connection.send_header('Connection', 'close')
connection.end_headers()
connection.wfile.write(exported)
示例9: _write_part
# 需要导入模块: import zlib [as 别名]
# 或者: from zlib import compressobj [as 别名]
def _write_part(self):
z = zlib.compressobj(6, zlib.DEFLATED, zlib.MAX_WBITS + 16)
offset = self.gzip_temp.tell()
buff = '\n'.join(self.curr_lines) + '\n'
self.curr_lines = []
buff = z.compress(buff)
self.gzip_temp.write(buff)
buff = z.flush()
self.gzip_temp.write(buff)
self.gzip_temp.flush()
length = self.gzip_temp.tell() - offset
partline = '{0}\t{1}\t{2}\t{3}'.format(self.curr_key, self.part_name, offset, length)
return partline
示例10: _encode_packobj
# 需要导入模块: import zlib [as 别名]
# 或者: from zlib import compressobj [as 别名]
def _encode_packobj(type, content, compression_level=1):
szout = ''
sz = len(content)
szbits = (sz & 0x0f) | (_typemap[type]<<4)
sz >>= 4
while 1:
if sz: szbits |= 0x80
szout += chr(szbits)
if not sz:
break
szbits = sz & 0x7f
sz >>= 7
if compression_level > 9:
compression_level = 9
elif compression_level < 0:
compression_level = 0
z = zlib.compressobj(compression_level)
yield szout
yield z.compress(content)
yield z.flush()
示例11: test_send_message
# 需要导入模块: import zlib [as 别名]
# 或者: from zlib import compressobj [as 别名]
def test_send_message(self):
compress = zlib.compressobj(
zlib.Z_DEFAULT_COMPRESSION, zlib.DEFLATED, -zlib.MAX_WBITS)
extension = common.ExtensionParameter(common.DEFLATE_FRAME_EXTENSION)
request = _create_request_from_rawdata(
'', deflate_frame_request=extension)
msgutil.send_message(request, 'Hello')
msgutil.send_message(request, 'World')
expected = ''
compressed_hello = compress.compress('Hello')
compressed_hello += compress.flush(zlib.Z_SYNC_FLUSH)
compressed_hello = compressed_hello[:-4]
expected += '\xc1%c' % len(compressed_hello)
expected += compressed_hello
compressed_world = compress.compress('World')
compressed_world += compress.flush(zlib.Z_SYNC_FLUSH)
compressed_world = compressed_world[:-4]
expected += '\xc1%c' % len(compressed_world)
expected += compressed_world
self.assertEqual(expected, request.connection.written_data())
示例12: test_send_message_no_context_takeover_parameter
# 需要导入模块: import zlib [as 别名]
# 或者: from zlib import compressobj [as 别名]
def test_send_message_no_context_takeover_parameter(self):
compress = zlib.compressobj(
zlib.Z_DEFAULT_COMPRESSION, zlib.DEFLATED, -zlib.MAX_WBITS)
extension = common.ExtensionParameter(common.DEFLATE_FRAME_EXTENSION)
extension.add_parameter('no_context_takeover', None)
request = _create_request_from_rawdata(
'', deflate_frame_request=extension)
for i in xrange(3):
msgutil.send_message(request, 'Hello')
compressed_message = compress.compress('Hello')
compressed_message += compress.flush(zlib.Z_SYNC_FLUSH)
compressed_message = compressed_message[:-4]
expected = '\xc1%c' % len(compressed_message)
expected += compressed_message
self.assertEqual(
expected + expected + expected, request.connection.written_data())
示例13: compress
# 需要导入模块: import zlib [as 别名]
# 或者: from zlib import compressobj [as 别名]
def compress(body, compress_level):
"""Compress 'body' at the given compress_level."""
import zlib
# See http://www.gzip.org/zlib/rfc-gzip.html
yield b'\x1f\x8b' # ID1 and ID2: gzip marker
yield b'\x08' # CM: compression method
yield b'\x00' # FLG: none set
# MTIME: 4 bytes
yield struct.pack('<L', int(time.time()) & int('FFFFFFFF', 16))
yield b'\x02' # XFL: max compression, slowest algo
yield b'\xff' # OS: unknown
crc = zlib.crc32(b'')
size = 0
zobj = zlib.compressobj(compress_level,
zlib.DEFLATED, -zlib.MAX_WBITS,
zlib.DEF_MEM_LEVEL, 0)
for line in body:
size += len(line)
crc = zlib.crc32(line, crc)
yield zobj.compress(line)
yield zobj.flush()
# CRC32: 4 bytes
yield struct.pack('<L', crc & int('FFFFFFFF', 16))
# ISIZE: 4 bytes
yield struct.pack('<L', size & int('FFFFFFFF', 16))
示例14: _create_compressor
# 需要导入模块: import zlib [as 别名]
# 或者: from zlib import compressobj [as 别名]
def _create_compressor(self):
return zlib.compressobj(tornado.web.GZipContentEncoding.GZIP_LEVEL,
zlib.DEFLATED, -self._max_wbits)
示例15: _write_block
# 需要导入模块: import zlib [as 别名]
# 或者: from zlib import compressobj [as 别名]
def _write_block(self, block):
"""Write provided data to file as a single BGZF compressed block (PRIVATE)."""
# print("Saving %i bytes" % len(block))
start_offset = self._handle.tell()
assert len(block) <= 65536
# Giving a negative window bits means no gzip/zlib headers,
# -15 used in samtools
c = zlib.compressobj(self.compresslevel,
zlib.DEFLATED,
-15,
zlib.DEF_MEM_LEVEL,
0)
compressed = c.compress(block) + c.flush()
del c
assert len(compressed) < 65536, \
"TODO - Didn't compress enough, try less data in this block"
crc = zlib.crc32(block)
# Should cope with a mix of Python platforms...
if crc < 0:
crc = struct.pack("<i", crc)
else:
crc = struct.pack("<I", crc)
bsize = struct.pack("<H", len(compressed) + 25) # includes -1
crc = struct.pack("<I", zlib.crc32(block) & 0xffffffff)
uncompressed_length = struct.pack("<I", len(block))
# Fixed 16 bytes,
# gzip magic bytes (4) mod time (4),
# gzip flag (1), os (1), extra length which is six (2),
# sub field which is BC (2), sub field length of two (2),
# Variable data,
# 2 bytes: block length as BC sub field (2)
# X bytes: the data
# 8 bytes: crc (4), uncompressed data length (4)
data = _bgzf_header + bsize + compressed + crc + uncompressed_length
self._handle.write(data)