zlib
와 내가 압축하고 문자열과 사전 전원 와이 스크립트입니다 :zlib 압축 문자열에서 헤더를 제거하는 방법은 무엇입니까?
import zlib
__doc__ = """
rly this small py em ell thon... !? ha he hahaha hehehe h4ck3r wassup how ar u let's go lets go common c'mon...!!! ad
"""
class Compressor(object):
def __init__(self, seed):
c = zlib.compressobj(9)
d_seed = c.compress(seed)
d_seed += c.flush(zlib.Z_SYNC_FLUSH)
self.c_context = c.copy()
d = zlib.decompressobj()
d.decompress(d_seed)
while d.unconsumed_tail:
d.decompress(d.unconsumed_tail)
self.d_context = d.copy()
def compress(self, text):
c = self.c_context.copy()
t = c.compress(text)
t2 = c.flush(zlib.Z_FINISH)
return t + t2
def decompress(self, ctext):
d = self.d_context.copy()
t = d.decompress(ctext)
while d.unconsumed_tail:
t += d.decompress(d.unconsumed_tail)
return t
c = Compressor(__doc__)
compressed_string = c.compress(string)
print compressed_string
string = "I installed python, and it's very nice and easy to use!"
compressed = c.compress(string)
print c.decompress(compressed)
내 질문은 : 결과 compressed_string
에서 당신이 등으로 여분의 '커프'를 제거 어떻게 헤더와 마지막 4 개의 ADLER 바이트는 압축을 풀 때 나중에 추가됩니다. 일부 정의가 잘못되었을 때 수정하십시오. ZLIB 같이 뭔가 압축을해야 할 때 h3d78e23gdh278qs98qwjsj89qs
된다 (12
은 이론적 2 바이트 헤더와 1234
문자열의 끝에서 이론적 ADLER
부분) 한 후 다시 빌드 12h3d78e23gdh278qs98qwjsj89qs1234
처럼
to_decompress = '12',compressed,'1234'
c.decompress(to_decompress)
배경 : OP는 체크섬을 무시하고 가능한 압축 된 문자열 (작은 문자열의 압축 비율을 추적)을 압축하여 생성하려고했습니다. 그들은 [Python 대화방] (http://chat.stackoverflow.com/rooms/6/python)에서 목표를 논의하고있었습니다. –
compressobj & decompressobj의 zdict 인수는 파이썬 3.3 이상에서만 사용할 수 있습니다. –
고마워요. 말하는. –