本文整理汇总了Python中zlib.DEF_MEM_LEVEL属性的典型用法代码示例。如果您正苦于以下问题:Python zlib.DEF_MEM_LEVEL属性的具体用法?Python zlib.DEF_MEM_LEVEL怎么用?Python zlib.DEF_MEM_LEVEL使用的例子?那么恭喜您, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类zlib
的用法示例。
在下文中一共展示了zlib.DEF_MEM_LEVEL属性的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: gzip_app_iter
# 需要导入模块: import zlib [as 别名]
# 或者: from zlib import DEF_MEM_LEVEL [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 DEF_MEM_LEVEL [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: gzipStream
# 需要导入模块: import zlib [as 别名]
# 或者: from zlib import DEF_MEM_LEVEL [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)
示例4: compress_readable_output
# 需要导入模块: import zlib [as 别名]
# 或者: from zlib import DEF_MEM_LEVEL [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)
示例5: ibytes2icompressed
# 需要导入模块: import zlib [as 别名]
# 或者: from zlib import DEF_MEM_LEVEL [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)
示例6: _encode
# 需要导入模块: import zlib [as 别名]
# 或者: from zlib import DEF_MEM_LEVEL [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
示例7: compress
# 需要导入模块: import zlib [as 别名]
# 或者: from zlib import DEF_MEM_LEVEL [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))
示例8: _write_block
# 需要导入模块: import zlib [as 别名]
# 或者: from zlib import DEF_MEM_LEVEL [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)
示例9: __init__
# 需要导入模块: import zlib [as 别名]
# 或者: from zlib import DEF_MEM_LEVEL [as 别名]
def __init__(self, filename, mode="rb", compresslevel=9):
# This lock must be recursive, so that BufferedIOBase's
# readline(), readlines() and writelines() don't deadlock.
self._lock = RLock()
self._fp = None
self._closefp = False
self._mode = _MODE_CLOSED
self._pos = 0
self._size = -1
if not isinstance(compresslevel, int) or not (1 <= compresslevel <= 9):
raise ValueError("'compresslevel' must be an integer "
"between 1 and 9. You provided 'compresslevel={}'"
.format(compresslevel))
if mode == "rb":
mode_code = _MODE_READ
self._decompressor = zlib.decompressobj(self.wbits)
self._buffer = b""
self._buffer_offset = 0
elif mode == "wb":
mode_code = _MODE_WRITE
self._compressor = zlib.compressobj(compresslevel,
zlib.DEFLATED,
self.wbits,
zlib.DEF_MEM_LEVEL,
0)
else:
raise ValueError("Invalid mode: %r" % (mode,))
if isinstance(filename, _basestring):
self._fp = io.open(filename, mode)
self._closefp = True
self._mode = mode_code
elif hasattr(filename, "read") or hasattr(filename, "write"):
self._fp = filename
self._mode = mode_code
else:
raise TypeError("filename must be a str or bytes object, "
"or a file")
示例10: deflateStream
# 需要导入模块: import zlib [as 别名]
# 或者: from zlib import DEF_MEM_LEVEL [as 别名]
def deflateStream(input, compressLevel=6):
# NOTE: this produces RFC-conformant but some-browser-incompatible output.
# The RFC says that you're supposed to output zlib-format data, but many
# browsers expect raw deflate output. Luckily all those browsers support
# gzip, also, so they won't even see deflate output.
compress = zlib.compressobj(compressLevel, zlib.DEFLATED, zlib.MAX_WBITS, zlib.DEF_MEM_LEVEL, 0)
_compress = compress.compress
yield input.wait
for buf in input:
if len(buf) != 0:
yield _compress(buf)
yield input.wait
yield compress.flush()
示例11: compress
# 需要导入模块: import zlib [as 别名]
# 或者: from zlib import DEF_MEM_LEVEL [as 别名]
def compress(data):
"""
Compresses the given data with the DEFLATE algorithm. The first four bytes contain the length of the
uncompressed data.
:param data: uncompressed data
:type data: bytes or str
:return: compressed data
:rtype: bytes
"""
compress_object = zlib.compressobj(
zlib.Z_BEST_COMPRESSION,
zlib.DEFLATED,
zlib.MAX_WBITS,
zlib.DEF_MEM_LEVEL,
zlib.Z_DEFAULT_STRATEGY)
if type(data) == str:
compressed_data = compress_object.compress(data.encode('utf-8'))
compressed_data += compress_object.flush()
return struct.pack('!I', len(data.encode('utf-8'))) + compressed_data
elif type(data) == bytes:
compressed_data = compress_object.compress(data)
compressed_data += compress_object.flush()
return struct.pack('!I', len(data)) + compressed_data
else:
raise TypeError("Please pass a str or bytes to the packer.")
示例12: compress
# 需要导入模块: import zlib [as 别名]
# 或者: from zlib import DEF_MEM_LEVEL [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 ntob('\x1f\x8b') # ID1 and ID2: gzip marker
yield ntob('\x08') # CM: compression method
yield ntob('\x00') # FLG: none set
# MTIME: 4 bytes
yield struct.pack("<L", int(time.time()) & int('FFFFFFFF', 16))
yield ntob('\x02') # XFL: max compression, slowest algo
yield ntob('\xff') # OS: unknown
crc = zlib.crc32(ntob(""))
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))
示例13: compress
# 需要导入模块: import zlib [as 别名]
# 或者: from zlib import DEF_MEM_LEVEL [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 ntob('\x1f\x8b') # ID1 and ID2: gzip marker
yield ntob('\x08') # CM: compression method
yield ntob('\x00') # FLG: none set
# MTIME: 4 bytes
yield struct.pack('<L', int(time.time()) & int('FFFFFFFF', 16))
yield ntob('\x02') # XFL: max compression, slowest algo
yield ntob('\xff') # OS: unknown
crc = zlib.crc32(ntob(''))
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: __init__
# 需要导入模块: import zlib [as 别名]
# 或者: from zlib import DEF_MEM_LEVEL [as 别名]
def __init__(self, filename, mode="rb", compresslevel=9):
# This lock must be recursive, so that BufferedIOBase's
# readline(), readlines() and writelines() don't deadlock.
self._lock = RLock()
self._fp = None
self._closefp = False
self._mode = _MODE_CLOSED
self._pos = 0
self._size = -1
if not isinstance(compresslevel, int) or not (1 <= compresslevel <= 9):
raise ValueError("compresslevel must be between an integer "
"between 1 and 9, you gave {0}"
.format(compresslevel))
if mode == "rb":
mode_code = _MODE_READ
self._decompressor = zlib.decompressobj(self.wbits)
self._buffer = b""
self._buffer_offset = 0
elif mode == "wb":
mode_code = _MODE_WRITE
self._compressor = zlib.compressobj(compresslevel,
zlib.DEFLATED,
self.wbits,
zlib.DEF_MEM_LEVEL,
0)
else:
raise ValueError("Invalid mode: %r" % (mode,))
if isinstance(filename, _basestring):
self._fp = open(filename, mode)
self._closefp = True
self._mode = mode_code
elif hasattr(filename, "read") or hasattr(filename, "write"):
self._fp = filename
self._mode = mode_code
else:
raise TypeError("filename must be a str or bytes object, "
"or a file")
示例15: filter
# 需要导入模块: import zlib [as 别名]
# 或者: from zlib import DEF_MEM_LEVEL [as 别名]
def filter(self, handler):
is_local_client = handler.client_address[0] in ('127.0.0.1', '::1')
pacfile = os.path.join(os.path.dirname(os.path.abspath(__file__)), common.PAC_FILE)
urlparts = urlparse.urlsplit(handler.path)
if handler.command == 'GET' and urlparts.path.lstrip('/') == common.PAC_FILE:
if urlparts.query == 'flush':
if is_local_client:
thread.start_new_thread(PacUtil.update_pacfile, (pacfile,))
else:
return 'mock', {'status': 403, 'headers': {'Content-Type': 'text/plain'}, 'body': 'client address %r not allowed' % handler.client_address[0]}
if time.time() - os.path.getmtime(pacfile) > common.PAC_EXPIRED:
# check system uptime > 30 minutes
uptime = get_uptime()
if uptime and uptime > 1800:
thread.start_new_thread(lambda: os.utime(pacfile, (time.time(), time.time())) or PacUtil.update_pacfile(pacfile), tuple())
with open(pacfile, 'rb') as fp:
content = fp.read()
if not is_local_client:
serving_addr = urlparts.hostname or ProxyUtil.get_listen_ip()
content = content.replace('127.0.0.1', serving_addr)
headers = {'Content-Type': 'text/plain'}
if 'gzip' in handler.headers.get('Accept-Encoding', ''):
headers['Content-Encoding'] = 'gzip'
compressobj = zlib.compressobj(zlib.Z_DEFAULT_COMPRESSION, zlib.DEFLATED, -zlib.MAX_WBITS, zlib.DEF_MEM_LEVEL, 0)
dataio = io.BytesIO()
dataio.write('\x1f\x8b\x08\x00\x00\x00\x00\x00\x02\xff')
dataio.write(compressobj.compress(content))
dataio.write(compressobj.flush())
dataio.write(struct.pack('<LL', zlib.crc32(content) & 0xFFFFFFFFL, len(content) & 0xFFFFFFFFL))
content = dataio.getvalue()
return 'mock', {'status': 200, 'headers': headers, 'body': content}