如果有人仍在 python 3 中寻找 pako 等效方法(未在 python 2 中测试)。
python 中等效的 pako.deflate() 方法:
def pako_deflate(data):
compress = zlib.compressobj(zlib.Z_DEFAULT_COMPRESSION, zlib.DEFLATED, 15,
memLevel=8, strategy=zlib.Z_DEFAULT_STRATEGY)
compressed_data = compress.compress(js_string_to_byte(js_encode_uri_component(data)))
compressed_data += compress.flush()
return compressed_data
python 中的 pako.deflateRaw() 等价物:
def pako_deflate_raw(data):
compress = zlib.compressobj(
zlib.Z_DEFAULT_COMPRESSION, zlib.DEFLATED, -15, memLevel=8,
strategy=zlib.Z_DEFAULT_STRATEGY)
compressed_data = compress.compress(js_string_to_byte(js_encode_uri_component(data)))
compressed_data += compress.flush()
return compressed_data
pako.inflate() 方法等效:
def pako_inflate(data):
decompress = zlib.decompressobj(15)
decompressed_data = decompress.decompress(data)
decompressed_data += decompress.flush()
return decompressed_data
pako.inflateRaw() 方法等效:
def pako_inflate_raw(data):
decompress = zlib.decompressobj(-15)
decompressed_data = decompress.decompress(data)
decompressed_data += decompress.flush()
return decompressed_data
上述函数中使用的一些实用函数是:
from urllib.parse import quote, unquote
import base64
def js_encode_uri_component(data):
return quote(data, safe='~()*!.\'')
def js_decode_uri_component(data):
return unquote(data)
def js_string_to_byte(data):
return bytes(data, 'iso-8859-1')
def js_bytes_to_string(data):
return data.decode('iso-8859-1')
def js_btoa(data):
return base64.b64encode(data)
def js_atob(data):
return base64.b64decode(data)