You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python解压Zip文件报错NotImplementedError:大文件压缩方法不支持

问题描述

尝试用Python脚本批量解压Zip文件,执行如下代码时仅在处理大Zip文件时触发错误,小文件可正常解压,且所有Zip文件的创建方式一致:

import zipfile
with zipfile.ZipFile(FILE_PATH) as zfile:
    zfile.extractall(OUTPUT_FILE_PATH) 

报错信息:

---------------------------------------------------------------------------
NotImplementedError                       Traceback (most recent call last)
<ipython-input-7-ead33468a516> in <module>
      1 with zipfile.ZipFile(FILE_PATH) as zip_ref:
----> 2     zip_ref.extractall(OUTPUT_FILE_PATH)

~\anaconda3\lib\zipfile.py in extractall(self, path, members, pwd)
   1645 
   1646         for zipinfo in members:
-> 1647             self._extract_member(zipinfo, path, pwd)
   1648 
   1649     @classmethod

~\anaconda3\lib\zipfile.py in _extract_member(self, member, targetpath, pwd)
   1698             return targetpath
   1699 
-> 1700         with self.open(member, pwd=pwd) as source, \
   1701              open(targetpath, "wb") as target:
   1702             shutil.copyfileobj(source, target)

~\anaconda3\lib\zipfile.py in open(self, name, mode, pwd, force_zip64)
   1569                 pwd = None
   1570 
-> 1571             return ZipExtFile(zef_file, mode, zinfo, pwd, True)
   1572         except:
   1573             zef_file.close()

~\anaconda3\lib\zipfile.py in __init__(self, fileobj, mode, zipinfo, pwd, close_fileobj)
    817         self._left = zipinfo.file_size
    818 
-> 819         self._decompressor = _get_decompressor(self._compress_type)
    820 
    821         self._eof = False

~\anaconda3\lib\zipfile.py in _get_decompressor(compress_type)
    718 
    719 def _get_decompressor(compress_type):
-> 720     _check_compression(compress_type)
    721     if compress_type == ZIP_STORED:
    722         return None

~\anaconda3\lib\zipfile.py in _check_compression(compression)
    698                 "Compression requires the (missing) lzma module")
    699     else:
-> 700         raise NotImplementedError("That compression method is not supported")
    701 
    702 

NotImplementedError: That compression method is not supported
解决方案

1. 确认Zip文件的压缩算法

大文件可能被使用了Python标准库zipfile不支持的压缩算法。先检查目标Zip文件的压缩类型:

import zipfile

with zipfile.ZipFile(FILE_PATH, 'r') as zf:
    for info in zf.infolist():
        print(f"文件: {info.filename}, 压缩类型: {info.compress_type}")

对照zipfile官方支持的压缩类型常量:

  • zipfile.ZIP_STORED(0,无压缩)
  • zipfile.ZIP_DEFLATED(8,DEFLATE算法,默认支持)
  • zipfile.ZIP_BZIP2(12,需依赖bz2模块)
  • zipfile.ZIP_LZMA(14,需依赖lzma模块)
    如果输出的compress_type不在上述范围内,说明是标准库不兼容的算法。

2. 补全缺失的依赖模块

如果压缩类型是ZIP_BZIP2或ZIP_LZMA,对应安装缺失模块:

  • ZIP_BZIP2:Anaconda环境一般自带bz2,若缺失可通过系统包管理器安装(如Ubuntu执行sudo apt install python3-bz2)
  • ZIP_LZMA:Anaconda环境执行:
    conda install -c conda-forge python-lzma
    
    pip环境执行:
    pip install backports.lzma
    

3. 改用第三方兼容库

如果标准库无法处理,使用patool库——它会调用系统底层的解压工具(如7-Zip、unzip),支持几乎所有常见压缩格式:
先安装库:

pip install patool

替换解压代码:

import patoolib

patoolib.extract(FILE_PATH, outdir=OUTPUT_FILE_PATH)

4. 验证大Zip文件完整性

大文件传输或存储时可能损坏,导致解压时识别错误的压缩类型。先尝试用系统自带解压工具(如7-Zip)手动解压,确认文件是否完好。

内容的提问来源于stack exchange,提问作者beetee

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.17 17:20:31