python用gzip压缩string字符串,支持python2.6

压缩

1
2
3
4
5
6
7
def gzip_compress(buf, compresslevel=6):
out = StringIO.StringIO()
f = gzip.GzipFile(fileobj=out, mode="w", compresslevel=compresslevel)
f.write(buf)
f.close()
res = out.getvalue()
return res

解压缩

1
2
3
4
5
6
def gzip_decompress(buf):
obj = StringIO.StringIO(buf)
f = gzip.GzipFile(fileobj=obj)
result = f.read()
f.close()
return result
文章目录
  1. 1. 压缩
  2. 2. 解压缩