本文整理汇总了Python中zlib.DEFLATED属性的典型用法代码示例。如果您正苦于以下问题:Python zlib.DEFLATED属性的具体用法?Python zlib.DEFLATED怎么用?Python zlib.DEFLATED使用的例子?那么恭喜您, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类zlib
的用法示例。
在下文中一共展示了zlib.DEFLATED属性的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: gzip_app_iter
# 需要导入模块: import zlib [as 别名]
# 或者: from zlib import DEFLATED [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: enc
# 需要导入模块: import zlib [as 别名]
# 或者: from zlib import DEFLATED [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)
示例3: handle
# 需要导入模块: import zlib [as 别名]
# 或者: from zlib import DEFLATED [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)
示例4: _write_part
# 需要导入模块: import zlib [as 别名]
# 或者: from zlib import DEFLATED [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
示例5: test_send_message
# 需要导入模块: import zlib [as 别名]
# 或者: from zlib import DEFLATED [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())
示例6: test_send_message_no_context_takeover_parameter
# 需要导入模块: import zlib [as 别名]
# 或者: from zlib import DEFLATED [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())
示例7: test_send_message_fragmented
# 需要导入模块: import zlib [as 别名]
# 或者: from zlib import DEFLATED [as 别名]
def test_send_message_fragmented(self):
extension = common.ExtensionParameter(
common.PERMESSAGE_DEFLATE_EXTENSION)
request = _create_request_from_rawdata(
'', permessage_deflate_request=extension)
msgutil.send_message(request, 'Hello', end=False)
msgutil.send_message(request, 'Goodbye', end=False)
msgutil.send_message(request, 'World')
compress = zlib.compressobj(
zlib.Z_DEFAULT_COMPRESSION, zlib.DEFLATED, -zlib.MAX_WBITS)
compressed_hello = compress.compress('Hello')
compressed_hello += compress.flush(zlib.Z_SYNC_FLUSH)
expected = '\x41%c' % len(compressed_hello)
expected += compressed_hello
compressed_goodbye = compress.compress('Goodbye')
compressed_goodbye += compress.flush(zlib.Z_SYNC_FLUSH)
expected += '\x00%c' % len(compressed_goodbye)
expected += compressed_goodbye
compressed_world = compress.compress('World')
compressed_world += compress.flush(zlib.Z_SYNC_FLUSH)
compressed_world = compressed_world[:-4]
expected += '\x80%c' % len(compressed_world)
expected += compressed_world
self.assertEqual(expected, request.connection.written_data())
示例8: test_send_message_fragmented_empty_last_frame
# 需要导入模块: import zlib [as 别名]
# 或者: from zlib import DEFLATED [as 别名]
def test_send_message_fragmented_empty_last_frame(self):
extension = common.ExtensionParameter(
common.PERMESSAGE_DEFLATE_EXTENSION)
request = _create_request_from_rawdata(
'', permessage_deflate_request=extension)
msgutil.send_message(request, 'Hello', end=False)
msgutil.send_message(request, '')
compress = zlib.compressobj(
zlib.Z_DEFAULT_COMPRESSION, zlib.DEFLATED, -zlib.MAX_WBITS)
compressed_hello = compress.compress('Hello')
compressed_hello += compress.flush(zlib.Z_SYNC_FLUSH)
expected = '\x41%c' % len(compressed_hello)
expected += compressed_hello
compressed_empty = compress.compress('')
compressed_empty += compress.flush(zlib.Z_SYNC_FLUSH)
compressed_empty = compressed_empty[:-4]
expected += '\x80%c' % len(compressed_empty)
expected += compressed_empty
self.assertEqual(expected, request.connection.written_data())
示例9: test_receive_message_deflate
# 需要导入模块: import zlib [as 别名]
# 或者: from zlib import DEFLATED [as 别名]
def test_receive_message_deflate(self):
compress = zlib.compressobj(
zlib.Z_DEFAULT_COMPRESSION, zlib.DEFLATED, -zlib.MAX_WBITS)
compressed_hello = compress.compress('Hello')
compressed_hello += compress.flush(zlib.Z_SYNC_FLUSH)
compressed_hello = compressed_hello[:-4]
data = '\xc1%c' % (len(compressed_hello) | 0x80)
data += _mask_hybi(compressed_hello)
# Close frame
data += '\x88\x8a' + _mask_hybi(struct.pack('!H', 1000) + 'Good bye')
extension = common.ExtensionParameter(
common.PERMESSAGE_DEFLATE_EXTENSION)
request = _create_request_from_rawdata(
data, permessage_deflate_request=extension)
self.assertEqual('Hello', msgutil.receive_message(request))
self.assertEqual(None, msgutil.receive_message(request))
示例10: gzipStream
# 需要导入模块: import zlib [as 别名]
# 或者: from zlib import DEFLATED [as 别名]
def gzipStream(input, compressLevel=6):
crc, size = zlib.crc32(''), 0
# magic header, compression method, no flags
header = '\037\213\010\000'
# timestamp
header += struct.pack('<L', 0)
# uh.. stuff
header += '\002\377'
yield header
compress = zlib.compressobj(compressLevel, zlib.DEFLATED, -zlib.MAX_WBITS, zlib.DEF_MEM_LEVEL, 0)
_compress = compress.compress
_crc32 = zlib.crc32
yield input.wait
for buf in input:
if len(buf) != 0:
crc = _crc32(buf, crc)
size += len(buf)
yield _compress(buf)
yield input.wait
yield compress.flush()
yield struct.pack('<LL', crc & 0xFFFFFFFFL, size & 0xFFFFFFFFL)
示例11: compress_readable_output
# 需要导入模块: import zlib [as 别名]
# 或者: from zlib import DEFLATED [as 别名]
def compress_readable_output(src_file, compress_level=6):
crc = zlib.crc32(b"")
size = 0
zobj = zlib.compressobj(compress_level, zlib.DEFLATED, -zlib.MAX_WBITS,
zlib.DEF_MEM_LEVEL, zlib.Z_DEFAULT_STRATEGY)
prefix_written = False
while True:
data = src_file.read(DEFAULT_BUFFER_SIZE)
if not data:
break
size += len(data)
crc = zlib.crc32(data, crc)
data = zobj.compress(data)
if not prefix_written:
prefix_written = True
data = gzip_prefix() + data
yield data
yield zobj.flush() + struct.pack(b"<LL", crc & CRC_MASK, size)
示例12: ibytes2icompressed
# 需要导入模块: import zlib [as 别名]
# 或者: from zlib import DEFLATED [as 别名]
def ibytes2icompressed(source):
yield (
b'\037\213\010\000' + # Gzip file, deflate, no filename
struct.pack('<L', long(time.time())) + # compression start time
b'\002\377' # maximum compression, no OS specified
)
crc = zlib.crc32(b"")
length = 0
compressor = zlib.compressobj(9, zlib.DEFLATED, -zlib.MAX_WBITS, zlib.DEF_MEM_LEVEL, 0)
for d in source:
crc = zlib.crc32(d, crc) & 0xffffffff
length += len(d)
chunk = compressor.compress(d)
if chunk:
yield chunk
yield compressor.flush()
yield struct.pack("<2L", crc, length & 0xffffffff)
示例13: _encode
# 需要导入模块: import zlib [as 别名]
# 或者: from zlib import DEFLATED [as 别名]
def _encode(self, data, con, path):
compressobj = zlib.compressobj(
6,
zlib.DEFLATED,
16 + 15,
zlib.DEF_MEM_LEVEL,
0
)
data = compressobj.compress(data)
data += compressobj.flush()
return data
# -------------------- Cipher Enums --------------------
# payload encryption method
# https://github.com/keepassxreboot/keepassxc/blob/8324d03f0a015e62b6182843b4478226a5197090/src/format/KeePass2.cpp#L24-L26
示例14: _post
# 需要导入模块: import zlib [as 别名]
# 或者: from zlib import DEFLATED [as 别名]
def _post(self, data, url, session=None, timeout=None):
session = session or self._session
timeout = timeout or self._timeout
_logger.debug('Raw datastream being sent: %s', pprint.pformat(data))
if self._compress:
uncompressed_bytes = len(data)
c = zlib.compressobj(_COMPRESSION_LEVEL, zlib.DEFLATED,
zlib.MAX_WBITS | 16)
data = c.compress(data) + c.flush()
_logger.debug('Compressed payload from %d to %d bytes',
uncompressed_bytes, len(data))
response = session.post(url, data=data, timeout=timeout)
_logger.debug('Sending to SignalFx %s (%d %s)',
'succeeded' if response.ok else 'failed',
response.status_code, response.text)
示例15: compress
# 需要导入模块: import zlib [as 别名]
# 或者: from zlib import DEFLATED [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))