简体   繁体   English

如何在烧瓶/Python上解压缩文件

[英]How to unzip a file on flask/Python

Hi is it possible for a user to upload a file using flask;嗨,用户可以使用烧瓶上传文件吗? user would select it from there computer, select submit, which would be downloaded to a ZIP file folder on webserver(local host) and unzip that file, and search for a certain file within that unzip file directory I have the functionality of the form down to upload it can't figuire out how to unzip the file and save its content in a folder用户将从那里的计算机中选择它,选择提交,它将被下载到网络服务器(本地主机)上的 ZIP 文件夹并解压缩该文件,并在该解压缩文件目录中搜索某个文件我具有表单的功能上传它无法弄清楚如何解压缩文件并将其内容保存在文件夹中

This may work for your problem:这可能适用于您的问题:

You'd used this first then,那你先用这个

response = make_response(log_file.text)

this for the second这是第二个

handle.write(response.content)

.content is "Content of the response, in bytes." .content 是“响应的内容,以字节为单位”。 .text is "Content of the response, in unicode." .text 是“响应的内容,以 unicode 表示。”

If you want a byte stream, use .content.如果您想要字节流,请使用 .content。

for further comprehension go to: Can't unzip file retrieved with Requests in Flask app or to: Unzipping files in Python为了进一步理解,请转到:无法解压缩使用 Flask 应用程序中的请求检索到的文件或:在 Python 中解压缩文件

One of them should work其中一个应该工作

its pure python unzip file它的纯python解压缩文件

import zipfile
with zipfile.ZipFile(path_to_zip_file, 'r') as zip_ref:
    zip_ref.extractall(directory_to_extract_to)

then you can use it in your flask app然后你可以在你的烧瓶应用程序中使用它

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM