1. 压缩成utf8格式的zip包

  1. def _zip_project_file(path, src_project_file_path):
  2. source_path = pathlib.Path(path)
  3. root_folder_name = source_path.name
  4. with zipfile.ZipFile(src_project_file_path, mode="w", compression=zipfile.ZIP_DEFLATED, compresslevel=6, allowZip64=True) as archive:
  5. for file_path in source_path.rglob("*"):
  6. if file_path.is_file():
  7. save_path = file_path.relative_to(source_path)
  8. arcname = os.path.join(root_folder_name, save_path)
  9. archive.write(file_path, arcname=arcname, compress_type=zipfile.ZIP_DEFLATED, compresslevel=6)

2. 检测压缩包中文文件名是否为utf8

  1. # 默认使用的编码为cp437
  2. def check_zip_encoding(zip_file_path):
  3. with zipfile.ZipFile(zip_file_path, 'r') as zip_ref:
  4. file_list = zip_ref.namelist()
  5. for file_name in file_list:
  6. encoded_name = file_name.encode('utf8')
  7. detected_encoding = chardet.detect(encoded_name)['encoding']
  8. print(f"File: {file_name}, Encoding: {detected_encoding}")
文档更新时间: 2023-11-29 02:27   作者:admin