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

如何解决BadZipFile: File is not a zip file错误?

解决BadZipFile: File is not a zip file错误

可能的原因及对应处理方案

  • 文件下载不完整
    Kaggle浏览器下载大文件时易因网络问题损坏,建议用Kaggle API重新下载:

    1. 在Kaggle账户生成API token(kaggle.json)并上传到Colab
    2. 执行以下命令配置并下载:
      !mkdir -p ~/.kaggle
      !cp kaggle.json ~/.kaggle/
      !chmod 600 ~/.kaggle/kaggle.json
      # 替换为目标数据集的Kaggle路径,示例格式:datasets/用户名/数据集名
      !kaggle datasets download -d 用户名/batuguntingkertas
      
  • 文件实际为其他压缩格式
    部分Kaggle数据集后缀标为.zip,实际是7z/rar格式,可尝试对应工具:
    若为7z格式:

    !pip install py7zr
    
    import py7zr
    with py7zr.SevenZipFile("batuguntingkertas.zip", mode='r') as z:
        z.extractall()
    

    若为rar格式:

    !apt-get install unrar
    !unrar x batuguntingkertas.zip
    
  • 验证文件是否为标准zip
    检查文件头魔数(标准zip魔数为504B0304):

    with open("batuguntingkertas.zip", 'rb') as f:
        print(f.read(4).hex())
    

    输出不符则说明文件并非标准zip,需按实际格式处理。

  • 尝试修复损坏的zip文件
    确认是zip但损坏时,用系统命令修复:

    !zip -FF batuguntingkertas.zip --out fixed_batuguntingkertas.zip
    

    修复后重新解压:

    from zipfile import ZipFile
    with ZipFile("fixed_batuguntingkertas.zip", 'r') as zip:
        zip.extractall()
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.10 17:25:21