Sov*_*iut 19 python zip google-app-engine in-memory
我有一个小项目,非常适合Google App Engine.实现它取决于生成ZIP文件并将其返回的能力.
由于App Engine的分布式特性,据我所知,ZIP文件无法在传统意义上"内存"创建.它基本上必须生成并在单个请求/响应周期中发送.
Python zip模块是否甚至存在于App Engine环境中?
myr*_*lav 32
zipfile在appengine中可用,并且返回的示例如下:
from contextlib import closing
from zipfile import ZipFile, ZIP_DEFLATED
from google.appengine.ext import webapp
from google.appengine.api import urlfetch
def addResource(zfile, url, fname):
# get the contents
contents = urlfetch.fetch(url).content
# write the contents to the zip file
zfile.writestr(fname, contents)
class OutZipfile(webapp.RequestHandler):
def get(self):
# Set up headers for browser to correctly recognize ZIP file
self.response.headers['Content-Type'] ='application/zip'
self.response.headers['Content-Disposition'] = \
'attachment; filename="outfile.zip"'
# compress files and emit them directly to HTTP response stream
with closing(ZipFile(self.response.out, "w", ZIP_DEFLATED)) as outfile:
# repeat this for every URL that should be added to the zipfile
addResource(outfile,
'https://www.google.com/intl/en/policies/privacy/',
'privacy.html')
addResource(outfile,
'https://www.google.com/intl/en/policies/terms/',
'terms.html')
Run Code Online (Sandbox Code Playgroud)
小智 9
import zipfile
import StringIO
text = u"ABCDEFGHIJKLMNOPQRSTUVWXYVabcdefghijklmnopqqstuvweyxáéöüï? ? ? ? ? ? ? ?"
zipstream=StringIO.StringIO()
file = zipfile.ZipFile(file=zipstream,compression=zipfile.ZIP_DEFLATED,mode="w")
file.writestr("data.txt.zip",text.encode("utf-8"))
file.close()
zipstream.seek(0)
self.response.headers['Content-Type'] ='application/zip'
self.response.headers['Content-Disposition'] = 'attachment; filename="data.txt.zip"'
self.response.out.write(zipstream.getvalue())
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8548 次 |
| 最近记录: |