Skip to content

Instantly share code, notes, and snippets.

@you21979
Created January 21, 2014 07:07
Show Gist options
  • Save you21979/8535609 to your computer and use it in GitHub Desktop.
Save you21979/8535609 to your computer and use it in GitHub Desktop.
python2.6でgzipのbuffer扱えないのでしょうがないからこんな感じでつかうらしい
from gzip import GzipFile
from StringIO import StringIO
class GZOStream:
def __init__(self):
self.sio = StringIO()
self.gzf = GzipFile(fileobj=self.sio, mode='wb')
def write(self,data):
self.gzf.write(data)
def finish(self):
self.gzf.close()
buf = self.sio.getvalue()
self.sio.close()
return buf
class GZIStream:
def __init__(self, buff):
self.sio = StringIO(buff)
self.gzf = GzipFile(fileobj=self.sio, mode='rb')
def read(self):
return self.gzf.read()
def close(self):
self.gzf.close()
self.sio.close()
go = GZOStream()
go.write("aiiiiaaa")
gi = GZIStream(go.finish())
print gi.read()
@you21979
Copy link
Author

python3ならこんな苦労はない

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment