import gzip import StringIO def gzip_compress(buf): out = StringIO.StringIO() with gzip.GzipFile(fileobj=out, mode="w") as f: f.write(buf) return out.getvalue() def gzip_decompress(buf): obj = StringIO.StringIO(buf) with gzip.GzipFile(fileobj=obj) as f: result = f.read() return result
转载于:https://my.oschina.net/u/1176097/blog/751805