我想动态创建一个zip文件,通过Cherry Py为用户提供服务:我尝试了下面生成无效zip文件的代码:
@cherrypy.expose
def ZipDir(self, *args):
path = "\\".join(args)
output = StringIO.StringIO()
file = zipfile.ZipFile(output, "w")
zip_filename = path + ".zip"
cherrypy.response.headers['Content-Type'] = 'application/zip'
cherrypy.response.headers['Content-Disposition'] = 'attachment; filename="%s"' % (zip_filename,)
dir = filter(lambda x: x.lower().endswith(".mp3") or not ("." in x), os.listdir("G:\\Music\\" + path))
for f in dir:
file.write("G:\\Music\\" + path + "\\" + f,f,zipfile.ZIP_DEFLATED)
return output.getvalue()
文件大小看起来是正确的,但它没有注册为任何acrhicing应用程序的zip文件。
答案 0 :(得分:1)
我忘了在返回之前调用file.close()。修好了!