读取发送到Flask服务器的zip文件而不将其存储在磁盘上 [英] Read a zip file sent to a flask server without storing it on disk

查看:57
本文介绍了读取发送到Flask服务器的zip文件而不将其存储在磁盘上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想读取通过表单发布请求发送到Flask服务器的特定类型zip文件中的所有文件,而不必将该zip文件存储在磁盘上.

I would like to read all the files in a zip file of a specific type sent to a flask server via a form post request without having to store the zip file on disk.

推荐答案

首先,获取获取zip文件的代码

First, get the code to get the zip file

from flask import Flask, request
app = Flask(__name__)

@app.route("/",methods=["GET"])
def page_name_get(): 
    return """<form action="." method="post" enctype=multipart/form-data>
        <input type="file" accept="application/zip" name="data_zip_file" accept="application/zip" required>
         <button type="submit">Send zip file!</button>
        </form>"""
app.run()

这是帖子请求功能的外观

This is how the post request function should look like

import zipfile

@app.route("/",methods=["POST"])
def page_name_post():
    file = request.files['data_zip_file']  
    file_like_object = file.stream._file  
    zipfile_ob = zipfile.ZipFile(file_like_object)
    file_names = zipfile_ob.namelist()
    # Filter names to only include the filetype that you want:
    file_names = [file_name for file_name in file_names if file_name.endswith(".txt")]
    files = [(zipfile_ob.open(name).read(),name) for name in file_names]
    return str(files)

现在我将逐行浏览

file = request.files ['data_zip_file'] 首先,您需要从请求中获取文件对象,这是 werkzeug.datastructures.FileStorage 的实例>课堂.

file = request.files['data_zip_file'] First, you need to get the file object from the request this is is an instance of the werkzeug.datastructures.FileStorage class.

file_like_object = file.stream._file ,在此首先获取 werkzeug.datastructures.FileStorage 的stream属性,这是文件的输入流.这将返回 tempfile.SpooledTemporaryFile 的实例,该实例用于临时文件.在该实例中,采用._file属性.这将返回 tempfile._TemporaryFileWrapper 的实例.这足以像zipfile.ZipFile类理解的 io.BytesIO 一样.

file_like_object = file.stream._file here you first take the stream attribute of the werkzeug.datastructures.FileStorage this is the input stream of the file. This will return an instance of tempfile.SpooledTemporaryFile a class used for temporary files. From that instance, you take the ._file attribute. This will return an instance of tempfile._TemporaryFileWrapper This is enough like an io.BytesIO to be understood by the zipfile.ZipFile class.

zipfile_ob = zipfile.ZipFile(file_like_object)在这里创建 zipfile.Zipfile 对象

现在,您应该可以执行几乎所有想要使用zip进行的操作.要从zip文件中选择文件,请使用 zipfile_ob.open()方法,然后将路径传递到您要打开的文件中.

Now you should be able to do pretty much everything you would want to do with the zip. To select a file from the zip use the zipfile_ob.open() method and pass in the path to the file you want to open.

要获取这些路径,我们使用 file_names = zipfile_ob.namelist(),这将返回一个列表,其中包含zip中所有文件和目录的所有路径的字符串.

To get those paths we use file_names = zipfile_ob.namelist() this will return a list with strings of all the paths to all the files and directories in the zip.

然后,您可以使用 file_names = [如果file_name.endswith(.txt")]在file_names中的file_name中的file_name过滤这些名称,

所有想要的路径现在都在 file_names 中.然后,您可以使用打开功能提取那些文件的数据.

All those paths you want are now in file_names. Then you can extract the data of those files using the open function.

文件= [[zipfile_ob.open(name).read(),name)作为文件名中的名称]

在给出的示例中,我将文件的路径保留在最终列表中,但是如果您不希望使用,可以使用:

In the example given I keep the paths to the file in the final list but if you don't want that you can use:

files = [zipfile_ob.open(name).read()表示文件名中的名称] 或使用其他方式查看该文件.如果您想了解有关文件的更多信息,也可以使用 infolist()方法代替 namelist()方法,这将返回 ZipInfo对象,而不是仅包含字符串的列表.这些对象包含有关所有文件的更多数据.

files = [zipfile_ob.open(name).read() for name in file_names] or use some other way to go over the file. If you want more info about the files there is also the infolist() method that can be used instead of the namelist() this will return a list of ZipInfo Objects instead of a list of just strings. These objects hold some more data about all the files.

这篇关于读取发送到Flask服务器的zip文件而不将其存储在磁盘上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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