代码示例
from flask import Flask,request
import zipfile
import datetime
# Flask
app = Flask(__name__)
@app.route("/judge", methods=["POST"])
def check():
file = request.files['file']
fileName = file.filename
if str(fileName).split('.')[1] != 'zip':
return {'code': '200', 'msg': '文件后缀名出现错误'}
try:
startNow = datetime.datetime.now()
zip_file = zipfile.ZipFile(file)
for names in zip_file.namelist(): # 解压
zip_file.extract(names, "ceshi1") # 解压后名称
zip_file.close()
endNow = datetime.datetime.now()
print(endNow - startNow)
return {'return_code': '200', 'return_info': '处理成功'}
except Exception as e:
pass
if __name__ == "__main__":
# Run
port = 5000
app.run(host='127.0.0.1', port=port, debug=False, use_reloader=False)
解压注意事项
1.解压中文时会产生乱码情况
在 zipfile文件内找到
if flags & 0x800:
# UTF-8 file names extension
filename = filename.decode('utf-8')
else:
try:
# Historical ZIP filename encoding
filename = filename.decode('cp437')
# 追加此句
filename = filename.encode("cp437").decode('gbk')
except:
zip_file = zip_file.encode('utf-8').decode('utf-8')
------------------------------------------------------------------------------------
if zinfo.flag_bits & 0x800:
# UTF-8 filename
fname_str = fname.decode("utf-8")
else:
try:
fname_str = fname.decode("cp437")
# 追加此句
fname_str = fname_str.encode("cp437").decode('gbk')
except:
zip_file = zip_file.encode('utf-8').decode('utf-8')
找到这两处if语句的地方进行修改