using python zipFile: strange 'BadZipfile'

using GAE with Python

wanna generate a zip file from the server with some server-generated rtf files.

 1 import zipfile
 2 from cStringIO import StringIO 
 3 
 4 zipStream = StringIO()
 5 = zipfile.ZipFile(zipStream, 'w', zipfile.ZIP_DEFLATED)
 6 
 7 #f = StringIO()
 8 #generated by another lib
 9 #f could be any string
10 z.writestr('test.rtf', f.getvalue())
11 z.close()
12 zipStream.seek(0)
13 self.response.out.write(zipStream.getvalue())

 someone said we could not use the return value if the ZipFile was created by passing in a file-like object

http://stackoverflow.com/questions/583791/is-it-possible-to-generate-and-return-a-zip-file-with-app-engine

 i found that we could add 

z.close()
zipStream.seek(0)

 then the 'BadZipfile' is gone.

 that's strange.

原文地址:https://www.cnblogs.com/liyinkan/p/2098789.html