在python Web应用程序中,我将一些东西打包成zip文件.我想在内存中完全执行此操作,而不需要触摸磁盘.只要我正在创建一个平面目录结构,使用ZipFile.writestr就可以了,但是如何在zip中创建目录呢?
我正在使用python2.4.
pth*_*lin 30
What 'theomega' said in the comment to my original post, adding a '/' in the filename does the trick. Thanks!
from zipfile import ZipFile
from StringIO import StringIO
inMemoryOutputFile = StringIO()
zipFile = ZipFile(inMemoryOutputFile, 'w')
zipFile.writestr('OEBPS/content.xhtml', 'hello world')
zipFile.close()
inMemoryOutputFile.seek(0)
Run Code Online (Sandbox Code Playgroud)