是否可以使用App Engine生成并返回ZIP文件? [英] Is it possible to generate and return a ZIP file with App Engine?

查看:199
本文介绍了是否可以使用App Engine生成并返回ZIP文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个适合Google App Engine的小项目。实现它取决于能否生成ZIP文件并将其返回。

I have a small project that would be perfect for Google App Engine. Implementing it hinges on the ability to generate a ZIP file and return it.

由于App Engine的分布式特性,从我所知道的情况来看,ZIP文件无法实现,在传统意义上被创建为内存中。它基本上必须在一个请求/响应周期中生成和发送。

Due to the distributed nature of App Engine, from what I can tell, the ZIP file couldn't be created "in-memory" in the traditional sense. It would basically have to be generated and and sent in a single request/response cycle.

Python App模块是否存在于App Engine环境中?

Does the Python zip module even exist in the App Engine environment?

推荐答案

zipfile 可在appengine上使用并重新制作示例 a>如下:

zipfile is available at appengine and reworked example follows:

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')

这篇关于是否可以使用App Engine生成并返回ZIP文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆