Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 如何在Flask中通过gzip解压缩FileStorage对象_Python 3.x_Flask_Httprequest_Gzip_File Storage - Fatal编程技术网

Python 3.x 如何在Flask中通过gzip解压缩FileStorage对象

Python 3.x 如何在Flask中通过gzip解压缩FileStorage对象,python-3.x,flask,httprequest,gzip,file-storage,Python 3.x,Flask,Httprequest,Gzip,File Storage,我正在通过HTTP多部分/表单数据POST请求向我的flask应用程序发送一个压缩文件(用gzip压缩)。请求工作正常,当我将密钥值打印到控制台时,它会打印以下内容:。这是使用Windows zip compressed压缩的,但将使用应用程序/gzip类型进行压缩。compressed.zip文件的键称为compressed。我的问题是如何解压缩gzip文件,然后将FileStorage对象存储到后端的特定路径?以下是我目前拥有的代码: from flask import request, B

我正在通过HTTP多部分/表单数据POST请求向我的flask应用程序发送一个压缩文件(用gzip压缩)。请求工作正常,当我将密钥值打印到控制台时,它会打印以下内容:
。这是使用Windows zip compressed压缩的,但将使用
应用程序/gzip
类型进行压缩。compressed.zip文件的键称为
compressed
。我的问题是如何解压缩gzip文件,然后将FileStorage对象存储到后端的特定路径?以下是我目前拥有的代码:

from flask import request, Blueprint, make_response
import gzip
blueprint = Blueprint("blueprint", __name__)

@blueprint.route("/compressed-post", methods=["POST"])
def compressed_post():
    if request.method == "POST":
        file = request.files["compressed"]
        print(file)
        return make_response("", 200)
    else:
        return make_response("", 400)

尝试使用以下方法:

from flask import request, Blueprint, make_response
from zipfile import ZipFile
import gzip
blueprint = Blueprint("blueprint", __name__)

@blueprint.route("/compressed-post", methods=["POST"])
def compressed_post():
    if request.method == "POST":
        file = request.files["compressed"]
        print(file)
        file_name = file.filename
        with ZipFile(file_name, 'r') as zip: 
            # printing all the contents of the zip file 
            zip.printdir() 
  
            # extracting all the files 
            print('Extracting all the files now...') 
            zip.extractall(<path_to_destination_dir>) 
            print('Done!')
        
        return make_response("", 200)
    else:
        return make_response("", 400)
从flask导入请求、蓝图,做出响应
从zipfile导入zipfile
导入gzip
蓝图=蓝图(“蓝图”,名称)
@blueprint.route(“/compressed post”,methods=[“post”])
def compressed_post():
如果request.method==“POST”:
file=request.files[“compressed”]
打印(文件)
file\u name=file.filename
ZipFile(文件名为“r”)作为zip:
#打印zip文件的所有内容
zip.printdir()
#提取所有文件
打印('正在提取所有文件…')
zip.extractall()
打印('Done!')
返回make_响应(“,200)
其他:
返回make_响应(“,400)
有关zip:和的更多详细信息,请参见此

有关文件存储对象的更多详细信息,请参见:

如果您需要更多信息来存储文件存储对象: 将文件保存到目标路径或文件对象。如果目标是文件对象,则必须在调用后自己关闭它。缓冲区大小是复制过程中内存中保留的字节数。它默认为16KB

见此:


我希望它能有所帮助。

尝试使用以下方法:

from flask import request, Blueprint, make_response
from zipfile import ZipFile
import gzip
blueprint = Blueprint("blueprint", __name__)

@blueprint.route("/compressed-post", methods=["POST"])
def compressed_post():
    if request.method == "POST":
        file = request.files["compressed"]
        print(file)
        file_name = file.filename
        with ZipFile(file_name, 'r') as zip: 
            # printing all the contents of the zip file 
            zip.printdir() 
  
            # extracting all the files 
            print('Extracting all the files now...') 
            zip.extractall(<path_to_destination_dir>) 
            print('Done!')
        
        return make_response("", 200)
    else:
        return make_response("", 400)
从flask导入请求、蓝图,做出响应
从zipfile导入zipfile
导入gzip
蓝图=蓝图(“蓝图”,名称)
@blueprint.route(“/compressed post”,methods=[“post”])
def compressed_post():
如果request.method==“POST”:
file=request.files[“compressed”]
打印(文件)
file\u name=file.filename
ZipFile(文件名为“r”)作为zip:
#打印zip文件的所有内容
zip.printdir()
#提取所有文件
打印('正在提取所有文件…')
zip.extractall()
打印('Done!')
返回make_响应(“,200)
其他:
返回make_响应(“,400)
有关zip:和的更多详细信息,请参见此

有关文件存储对象的更多详细信息,请参见:

如果您需要更多信息来存储文件存储对象: 将文件保存到目标路径或文件对象。如果目标是文件对象,则必须在调用后自己关闭它。缓冲区大小是复制过程中内存中保留的字节数。它默认为16KB

见此:


我希望它能有所帮助。

鉴于您的代码片段

@blueprint.route("/compressed-post", methods=["POST"])
def compressed_post():
    if request.method == "POST":
        file = request.files["compressed"]
。。。您只需传入
文件。_file
进入
ZipFile
构造函数,因为
ZipFile
需要一个类似文件的对象(或者一个完整的路径,我们这里没有,因为文件尚未保存到磁盘)

然后,您可以使用
ZipFile
s
extractAll
方法将文件提取到您喜欢的路径

也看到

因此,完整的代码应该是这样的:

from flask import request, Blueprint, make_response
import gzip
from zipfile import ZipFile
blueprint = Blueprint("blueprint", __name__)

@blueprint.route("/compressed-post", methods=["POST"])
def compressed_post():
    if request.method == "POST":
        file = request.files["compressed"]
        zip_handle = ZipFile(file._file)
        zip_handle.extractall(CHOOSE_YOUR_PATH)
        zip_handle.close()
        return make_response("", 200)
    else:
        return make_response("", 400)
        
ZipFile
也有一个contextmanager,如果您喜欢使用
语法


警告一句:
文件中的
。\u文件
表示
\u文件
不是公共界面的一部分,将来可能会更改。

给定您的代码片段

@blueprint.route("/compressed-post", methods=["POST"])
def compressed_post():
    if request.method == "POST":
        file = request.files["compressed"]
。。。您只需传入
文件。_file
进入
ZipFile
构造函数,因为
ZipFile
需要一个类似文件的对象(或者一个完整的路径,我们这里没有,因为文件尚未保存到磁盘)

然后,您可以使用
ZipFile
s
extractAll
方法将文件提取到您喜欢的路径

也看到

因此,完整的代码应该是这样的:

from flask import request, Blueprint, make_response
import gzip
from zipfile import ZipFile
blueprint = Blueprint("blueprint", __name__)

@blueprint.route("/compressed-post", methods=["POST"])
def compressed_post():
    if request.method == "POST":
        file = request.files["compressed"]
        zip_handle = ZipFile(file._file)
        zip_handle.extractall(CHOOSE_YOUR_PATH)
        zip_handle.close()
        return make_response("", 200)
    else:
        return make_response("", 400)
        
ZipFile
也有一个contextmanager,如果您喜欢使用
语法


注意:文件中的
。\u文件
表示,
\u文件
不是公共界面的一部分,将来可能会更改。

因此您希望解压缩gzip文件并将内容(所有文件都放在压缩文件中)在文件存储中?是的,这就是我想要做的。我很快就会上传一个答案。所以只需将zip文件中的所有文件放在某个文件夹中,
file
包含压缩文件,对吗?是的,非常多,但正如我前面提到的,
file
是一个文件存储对象,所以我不知道如何处理该数据类型。@你要上传答案吗?所以你想解压缩gzip文件并将内容(压缩文件中的所有文件)放入文件存储?是的,这就是我想做的。我很快就会上传答案。所以只需将zip文件中的所有文件放在某个文件夹中,
file
包含压缩文件,对吗?是的,但正如我前面提到的,
file
是一个文件存储对象,所以我不知道如何处理该数据类型。@Patch是否要上载答案?对于save()函数,如果我不知道缓冲区大小,假设它大于默认的16KB,该怎么办?如果该漏洞是可能的,我不想使缓冲区溢出。另外,我准确地使用了您的代码,并在这行代码的响应中使用ZipFile(file_name,'r')作为zip:
得到了一个带有
file\u name
变量的FileNotFoundError。您知道我为什么会出现此错误吗?我无法用