Index: gzip.py =================================================================== --- gzip.py (revision 76139) +++ gzip.py (working copy) @@ -8,8 +8,9 @@ import struct, sys, time, os import zlib import builtins +import io -__all__ = ["GzipFile","open"] +__all__ = ["GzipFile","open", "compress", "decompress"] FTEXT, FHCRC, FEXTRA, FNAME, FCOMMENT = 1, 2, 4, 8, 16 @@ -481,7 +482,25 @@ def __exit__(self, *args): self.close() +def compress(data, compresslevel=9) -> "compressed string": + """ Compress data in one shot. Returns the compressed string. + Optional argument is the compression level, in range of 1-9. """ + + bf = io.BytesIO(b'') + f = GzipFile(fileobj = bf, mode = 'wb', compresslevel = compresslevel) + f.write(data) + f.close() + return bf.getvalue() + +def decompress(data) -> "decompressed string": + """ Decompress a gzip compressed string in one shot. + Returns the decompressed string. """ + + f = GzipFile(fileobj = io.BytesIO(data)) + return f.read() + + def _test(): # Act like gzip; with -d, act like gunzip. # The input file is not deleted, however, nor are any other gzip