42 lines
1.1 KiB
Python
42 lines
1.1 KiB
Python
from gzip import GzipFile
|
|
|
|
class GzipWrap(object):
|
|
def __init__(self, input, filename=None, compresslevel=1):
|
|
self.input = iter(input)
|
|
self.buffer = ''
|
|
self.zipper = GzipFile(filename, mode='wb', fileobj=self, compresslevel=compresslevel)
|
|
|
|
def read(self, size=-1):
|
|
# If the buffer already has enough bytes, then simply pop them off of
|
|
# the beginning and return them.
|
|
if len(self.buffer) >= size:
|
|
ret = self.buffer[0:size]
|
|
self.buffer = self.buffer[size:]
|
|
return ret
|
|
|
|
# Otherwise, zip the input until we have enough bytes.
|
|
while True:
|
|
# Attempt to retrieve the next bytes to write.
|
|
is_done = False
|
|
try:
|
|
s = self.input.next()
|
|
self.zipper.write(s)
|
|
except StopIteration:
|
|
is_done = True
|
|
|
|
if len(self.buffer) < size or is_done:
|
|
self.zipper.flush()
|
|
|
|
if len(self.buffer) >= size or is_done:
|
|
ret = self.buffer[0:size]
|
|
self.buffer = self.buffer[size:]
|
|
return ret
|
|
|
|
def flush(self):
|
|
pass
|
|
|
|
def write(self, data):
|
|
self.buffer += data
|
|
|
|
def close(self):
|
|
self.input.close()
|