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
CodePudding user response:
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." .text is "Content of the response, in unicode."
If you want a byte stream, use .content.
for further comprehension go to: Can't unzip file retrieved with Requests in Flask app or to: Unzipping files in Python
One of them should work
CodePudding user response:
its pure python unzip file
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)