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 the current time by default.
(data, compresslevel=_COMPRESS_LEVEL_BEST, *, mtime=None)
| 574 | |
| 575 | |
| 576 | def compress(data, compresslevel=_COMPRESS_LEVEL_BEST, *, mtime=None): |
| 577 | """Compress data in one shot and return the compressed string. |
| 578 | |
| 579 | compresslevel sets the compression level in range of 0-9. |
| 580 | mtime can be used to set the modification time. The modification time is |
| 581 | set to the current time by default. |
| 582 | """ |
| 583 | if mtime == 0: |
| 584 | # Use zlib as it creates the header with 0 mtime by default. |
| 585 | # This is faster and with less overhead. |
| 586 | return zlib.compress(data, level=compresslevel, wbits=31) |
| 587 | header = _create_simple_gzip_header(compresslevel, mtime) |
| 588 | trailer = struct.pack("<LL", zlib.crc32(data), (len(data) & 0xffffffff)) |
| 589 | # Wbits=-15 creates a raw deflate block. |
| 590 | return (header + zlib.compress(data, level=compresslevel, wbits=-15) + |
| 591 | trailer) |
| 592 | |
| 593 | |
| 594 | def decompress(data): |
nothing calls this directly
no test coverage detected