fix: 修复单个文件用zip解压后文件名乱码的问题

This commit is contained in:
maxmon 2022-08-09 21:07:43 +08:00
parent c85261dce7
commit 45d1da4fd8
2 changed files with 10 additions and 3 deletions

View File

@ -7,7 +7,7 @@ from ...libs.redprint import RedPrint
from flask import request
import json
import os
from ...libs.tools import make_dir, write_json, read_json_file, unzip_file
from ...libs.tools import make_dir, write_json, read_json_file, unzip_file, get_cn_name
from ...entities.entities import Project, ReturnInfo
@ -57,8 +57,8 @@ def get_zipped_data():
shutil.copy(folder_path + '/' + file, folder_path + '/../')
# Here to deal with chinese encode in module zipfile and rarfile
if file_type == 'zip':
os.rename(folder_path + '/../' + file,
folder_path + '/../' + file.encode('cp437').decode('GBK'))
os.replace(folder_path + '/../' + file,
folder_path + '/../' + get_cn_name(file))
shutil.rmtree(folder_path)
except Exception as e:
print(e)

View File

@ -10,6 +10,11 @@ from os import listdir
from shutil import move
import base64
def get_cn_name(name):
try:
return name.encode('cp437').decode('GBK')
except:
return name
def make_dir(path):
folder = os.path.exists(path)
@ -72,6 +77,8 @@ def unzip_file(zip_src, dst_dir):
fz = zipfile.ZipFile(zip_src, "r")
for file in fz.namelist():
fz.extract(file, dst_dir)
# Here to deal with chinese encode in module zipfile and rarfile
os.replace(dst_dir + '/' + file, dst_dir + '/' + get_cn_name(file))
return "unzip .zip file success"
# elif rarfile.is_rarfile(zip_src):
# fr = rarfile.RarFile(zip_src, "r")