1. 压缩成utf8格式的zip包
def _zip_project_file(path, src_project_file_path):
source_path = pathlib.Path(path)
root_folder_name = source_path.name
with zipfile.ZipFile(src_project_file_path, mode="w", compression=zipfile.ZIP_DEFLATED, compresslevel=6, allowZip64=True) as archive:
for file_path in source_path.rglob("*"):
if file_path.is_file():
save_path = file_path.relative_to(source_path)
arcname = os.path.join(root_folder_name, save_path)
archive.write(file_path, arcname=arcname, compress_type=zipfile.ZIP_DEFLATED, compresslevel=6)
2. 检测压缩包中文文件名是否为utf8
# 默认使用的编码为cp437
def check_zip_encoding(zip_file_path):
with zipfile.ZipFile(zip_file_path, 'r') as zip_ref:
file_list = zip_ref.namelist()
for file_name in file_list:
encoded_name = file_name.encode('utf8')
detected_encoding = chardet.detect(encoded_name)['encoding']
print(f"File: {file_name}, Encoding: {detected_encoding}")
文档更新时间: 2023-11-29 02:27 作者:admin