Compress data in one shot and return the compressed string. compresslevel sets the compression level in range of 0-9. mtime can be used to set the modification time. The modification time is set to 0 by default, for reproducibility.
(data, compresslevel=_COMPRESS_LEVEL_TRADEOFF, *, mtime=0)
| 620 | |
| 621 | |
| 622 | def compress(data, compresslevel=_COMPRESS_LEVEL_TRADEOFF, *, mtime=0): |
| 623 | """Compress data in one shot and return the compressed string. |
| 624 | |
| 625 | compresslevel sets the compression level in range of 0-9. |
| 626 | mtime can be used to set the modification time. |
| 627 | The modification time is set to 0 by default, for reproducibility. |
| 628 | """ |
| 629 | # Wbits=31 automatically includes a gzip header and trailer. |
| 630 | gzip_data = zlib.compress(data, level=compresslevel, wbits=31) |
| 631 | if mtime is None: |
| 632 | mtime = time.time() |
| 633 | # Reuse gzip header created by zlib, replace mtime and OS byte for |
| 634 | # consistency. |
| 635 | header = struct.pack("<4sLBB", gzip_data, int(mtime), gzip_data[8], 255) |
| 636 | return header + gzip_data[10:] |
| 637 | |
| 638 | |
| 639 | def decompress(data): |