使用 Python 标准库 zipfile 在 App Engine 创建并返回 ZIP 文件

Google App Engine 是一种分布式平台,用于构建和部署基于 Python 的 web 应用。

  • 开发者需要在一个请求/响应周期内生成和发送 ZIP 文件,而不能以传统意义上的 “内存中” 的方式创建 ZIP文件。
  • App Engine 环境中是否存在 Python zip 模块。

2、解决方案

  • App Engine 中提供了 zipfile 模块,可以用来生成 ZIP 文件。
  • 使用 contextlib 模块的 closing 函数与 with 语句可以确保在处理文件和流时发生错误时正确关闭它们。
  • ZIP_DEFLATED 是 zipfile 模块的常量,用于指定 ZIP 文件的压缩格式。
  • set_content_type() 和 set_disposition() 方法可用于设置 HTTP 头信息,以确保浏览器正确识别 ZIP 文件。
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')
  • StringIO 是 Python 标准库中的模块,提供了 StringIO 类,该类是一个在内存中读写二进制数据的类,类似于一个文件对象。
  • zipfile.ZipFile 类可以用来创建一个 zip 文件,其中包含一个或多个压缩文件。
  • open() 方法可用来打开一个文件或流,并将该文件或流作为 zipfile.ZipFile 类构造函数的第一个参数。
  • mode 参数指定了 zip 文件的打开模式,“w” 表示以写入模式打开 zip 文件。compression 参数指定了 zip 文件的压缩方式,ZIP_DEFLATED 表示使用 DEFLATE 压缩算法对 zip 文件进行压缩。
  • writestr() 方法可用来向 zip 文件中写入一个字符串。
  • close() 方法可用来关闭 zip 文件。
  • self.response.headers[‘Content-Type’] =‘application/zip’ 和 self.response.headers[‘Content-Disposition’] = ‘attachment; filename=“data.txt.zip”’ 这两行代码用于设置 HTTP 头信息,以确保浏览器正确识别 ZIP 文件。
  • StringIO.getvalue() 方法可用来获取 StringIO 对象中存储的二进制数据。
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())
  • App Engine 文档指出,开发人员可以上传其他第三方库,只要它们是用纯 Python 实现的,并且不需要任何不受支持的标准库模块。
  • zip 模块是一个纯 Python 实现的库,因此可以将其上传到 App Engine。
  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值