Skip to content

Instantly share code, notes, and snippets.

@rectalogic
Created June 11, 2014 19:53
Show Gist options
  • Save rectalogic/b93722796efc9983f7d8 to your computer and use it in GitHub Desktop.
Save rectalogic/b93722796efc9983f7d8 to your computer and use it in GitHub Desktop.
import zipfile
import sys
class StreamFile(object):
def __init__(self, fileobj):
self.fileobj = fileobj
self.pos = 0
def write(self, str):
self.fileobj.write(str)
self.pos += len(str)
def tell(self):
return self.pos
def flush(self):
self.fileobj.flush()
# Wrap a stream so ZipFile can use it
out = StreamFile(sys.stdout)
z = zipfile.ZipFile(out, 'w', zipfile.ZIP_STORED)
z.writestr("hello1.txt", "this is hello1 contents\n")
z.writestr("hello2.txt", "this is hello2 contents\n")
z.writestr("hello3.txt", "this is hello3 contents\n")
z.writestr("hello4.txt", "this is hello4 contents\n")
z.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment