当前位置: 首页 > news >正文

py7zr解压文件时报错CrcError(crc32, f.crc32, f.filename)

报错信息

Traceback (most recent call last):File "/home/hp/project/test/file_util.py", line 130, in extract_archive_7zarchive.extract(targets=[fixed_file], path=output_dir, recursive=True)File "/home/hp/miniconda3/envs/celery/lib/python3.10/site-packages/py7zr/py7zr.py", line 1025, in extractself._extract(path, targets, return_dict=False, recursive=recursive)File "/home/hp/miniconda3/envs/celery/lib/python3.10/site-packages/py7zr/py7zr.py", line 634, in _extractself.worker.extract(File "/home/hp/miniconda3/envs/celery/lib/python3.10/site-packages/py7zr/py7zr.py", line 1266, in extractself.extract_single(File "/home/hp/miniconda3/envs/celery/lib/python3.10/site-packages/py7zr/py7zr.py", line 1354, in extract_singleraise eFile "/home/hp/miniconda3/envs/celery/lib/python3.10/site-packages/py7zr/py7zr.py", line 1351, in extract_singleself._extract_single(fp, files, path, src_end, q, skip_notarget)File "/home/hp/miniconda3/envs/celery/lib/python3.10/site-packages/py7zr/py7zr.py", line 1388, in _extract_singleself._check(fp, just_check, src_end)File "/home/hp/miniconda3/envs/celery/lib/python3.10/site-packages/py7zr/py7zr.py", line 1445, in _checkraise CrcError(crc32, f.crc32, f.filename)
py7zr.exceptions.CrcError: (599713723, 4127376704, 'valid/0001_JPEG.rf.9fcffd546df665288bd059e2975eb307.jpg')

代码如下:

def extract_archive_7z(archive_path: str, output_dir: str = ".", password: str = None):"""解压压缩文件使用 py7zr 解压 7z 格式压缩包,支持中文文件名。"""try:with py7zr.SevenZipFile(archive_path, mode='r', password=password) as archive:# 获取所有文件列表(用于进度条)all_files = archive.getnames()# 创建目标目录(若不存在)os.makedirs(output_dir, exist_ok=True)for file in tqdm(all_files, desc=f"解压 {os.path.basename(archive_path)}", unit="file"):fixed_file = fix_encoding(file)archive.extract(targets=[fixed_file], path=output_dir)logger.info(f"{archive_path} 解压完成")except Exception as e:logger.error(f"解压失败 {archive_path}: {e}", exc_info=True)

错误原因

py7zr 的archive.extract() 方法无法多次被调用

以下是修改后的代码:

def extract_archive_7z(archive_path: str, output_dir: str = ".", password: str = None):"""解压压缩文件使用 py7zr 解压 7z 格式压缩包,支持中文文件名。"""try:with py7zr.SevenZipFile(archive_path, mode='r', password=password) as archive:# 获取所有文件列表(用于进度条)all_files = archive.getnames()# 创建目标目录(若不存在)os.makedirs(output_dir, exist_ok=True)targets = []for file in tqdm(all_files, desc=f"解压 {os.path.basename(archive_path)}", unit="file"):targets = targets.append(file)archive.extract(targets=targets, path=output_dir) # 或使用extractAll()logger.info(f"{archive_path} 解压完成")except Exception as e:logger.error(f"解压失败 {archive_path}: {e}", exc_info=True)

参考:
https://github.com/miurahr/py7zr/issues/313

相关文章:

  • Kubernetes 入门笔记
  • 磁盘存储链式的 B 树与 B+ 树
  • 数据库原理实验:视图与索引
  • Python训练打卡Day23
  • Biba安全模型详解:守护信息系统完整性的基石
  • 网络安全-等级保护(等保) 2-3 GB/T 22240—2020《信息安全技术 网络安全等级保护定级指南》-2020-04-28发布【现行】
  • JAVA:synchronized总结
  • 大模型核心运行机制
  • C语言中的宏
  • Prometheus参数配置最佳实践
  • P1032 [NOIP 2002 提高组] 字串变换
  • shell脚本变量详解
  • 【WebApi】YiFeiWebApi接口安装说明
  • python: union()函数用法
  • uniapp+vue3开发项目之引入vuex状态管理工具
  • 内存泄漏系列专题分析之十三:高通相机CamX内存泄漏内存占用分析--Camx pipeline的ION内存拆解方法
  • 从 Vue3 回望 Vue2:响应式的内核革命
  • 【bag of n-grams】 N-gram词袋模型 简介
  • 已情感分析入门学习大模型-初级篇
  • 进程与线程:09 进程同步与信号量
  • 4月企业新发放贷款利率处于历史低位
  • 前四个月人民币贷款增加10.06万亿元,4月末M2余额同比增长8%
  • 颜福庆与顾临的争论:1930年代在中国维持一家医学院要花多少钱
  • 《蛮好的人生》:为啥人人都爱这个不完美的“大女主”
  • 中美是否计划讨论美方以芬太尼为由对华征收的特别关税?外交部回应
  • 演员黄晓明、金世佳进入上海戏剧学院2025年博士研究生复试名单