Last active
December 31, 2015 17:19
-
-
Save maliubiao/8019638 to your computer and use it in GitHub Desktop.
simple_gzip.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from time import time | |
| from struct import pack | |
| from zlib import compressobj | |
| from zlib import crc32 | |
| import zlib | |
| compresslevel = 9 | |
| _crc = crc32("") & 0xffffffffL | |
| compresser = compressobj(compresslevel, | |
| zlib.DEFLATED, | |
| -zlib.MAX_WBITS, | |
| zlib.DEF_MEM_LEVEL, | |
| 0) | |
| def set_compresslevel(compresslevel): | |
| global compresser | |
| compresser = compressobj(compresslevel, | |
| zlib.DEFLATED, | |
| -zlib.MAX_WBITS, | |
| zlib.DEF_MEM_LEVEL, | |
| 0) | |
| def write(src, dst): | |
| _compresser = compresser.copy() | |
| #header | |
| dst.write("\x1f\x8b\x08\x00") | |
| #write time now, 32bit unsigned | |
| dst.write(pack("<L", long(time()))) | |
| dst.write("\x02\xff") | |
| data = None | |
| if isinstance(src, str): | |
| data = src | |
| elif isinstance(src, unicode): | |
| data = src.encode("utf-8") | |
| else: | |
| data = src.getvalue() | |
| crc = crc32(data, _crc) & 0xffffffffL | |
| dst.write(_compresser.compress(data)) | |
| dst.write(_compresser.flush()) | |
| dst.write(pack("<L", crc)) | |
| dst.write(pack("<L", len(data) & 0xffffffffL)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment