1、 First, get the uploaded file from the front end
f = request.files['upload']
2、 Configuration save path
savepath = "/opt/upload/"
if not os.path.exists(savepath): # Determine whether the directory exists , If it doesn't exist, create a new one
os.makedirs(savepath)
3、 Save after configuring the path
upload_path = os.path.join(savepath, secure_filename(f.filename))
f.save(upload_path)
4、 Determine whether zip File and extract
if zipfile.is_zipfile(upload_path): # Determine whether zip file
zf = zipfile.ZipFile(upload_path, 'r') # Set the file to readable
stem, suffix = os.path.splitext(f.filename) # Extract file name
for file in zf.namelist(): # Traversal file
zf.extract(file, savepath + "/" + stem) # Extract to the specified directory