{"record":{"id":"be710764d383b8d0","repo":"d2l-ai/d2l-zh","slug":"zip-tar","errorCode":null,"errorMessage":"只有zip/tar文件可以被解压缩","messagePattern":"只有zip/tar文件可以被解压缩","errorType":"validation","errorClass":"AssertionError","httpStatus":null,"severity":"error","filePath":"d2l/mxnet.py","lineNumber":379,"sourceCode":"    print(f'正在从{url}下载{fname}...')\n    r = requests.get(url, stream=True, verify=True)\n    with open(fname, 'wb') as f:\n        f.write(r.content)\n    return fname\n\ndef download_extract(name, folder=None):\n    \"\"\"下载并解压zip/tar文件\n\n    Defined in :numref:`sec_kaggle_house`\"\"\"\n    fname = download(name)\n    base_dir = os.path.dirname(fname)\n    data_dir, ext = os.path.splitext(fname)\n    if ext == '.zip':\n        fp = zipfile.ZipFile(fname, 'r')\n    elif ext in ('.tar', '.gz'):\n        fp = tarfile.open(fname, 'r')\n    else:\n        assert False, '只有zip/tar文件可以被解压缩'\n    fp.extractall(base_dir)\n    return os.path.join(base_dir, folder) if folder else data_dir\n\ndef download_all():\n    \"\"\"下载DATA_HUB中的所有文件\n\n    Defined in :numref:`sec_kaggle_house`\"\"\"\n    for name in DATA_HUB:\n        download(name)\n\nDATA_HUB['kaggle_house_train'] = (\n    DATA_URL + 'kaggle_house_pred_train.csv',\n    '585e9cc93e70b39160e7921475f9bcd7d31219ce')\n\nDATA_HUB['kaggle_house_test'] = (\n    DATA_URL + 'kaggle_house_pred_test.csv',\n    'fa19780a7b011d9b009e8bff8e99922a8ee2eb90')\n","sourceCodeStart":361,"sourceCodeEnd":397,"githubUrl":"https://github.com/d2l-ai/d2l-zh/blob/e6b18ccea71451a55fcd861d7b96fddf2587b09a/d2l/mxnet.py#L361-L397","documentation":"AssertionError from d2l.mxnet.download_extract: after os.path.splitext on the downloaded filename, only '.zip' (ZipFile) and '.tar'/'.gz' (tarfile) extensions are supported; any other extension hits assert False with the Chinese message 'only zip/tar files can be extracted'. It is a format whitelist for the Kaggle-house style extraction helper.","triggerScenarios":"Registering a DATA_HUB entry whose URL ends in '.csv', '.json', '.txt', '.rar', or '.7z' and then calling download_extract on it; also '.tgz' or '.bz2' files, which the splitext check does not recognize even though tarfile could open them.","commonSituations":"Users reuse the kaggle-house boilerplate for their own dataset and forget the archive-format constraint; files whose true format is zip but named without the .zip extension; downloading raw '.gz' text files expecting auto-extraction of the inner file.","solutions":["Package your data as .zip or .tar/.gz and update the DATA_HUB URL to match","If the file is already uncompressed (csv/json), call d2l.download(name) directly instead of download_extract","Rename a mislabeled archive (e.g. file with no extension -> file.zip) after confirming its real format with `file`","For .tgz/.bz2, extend the check locally: `elif ext in ('.tar', '.gz', '.tgz', '.bz2')` or extract with tarfile.open(fname, 'r:*') yourself"],"exampleFix":"# before\nDATA_HUB['mydata'] = (DATA_URL + 'mydata.csv', sha1)\nd2l.download_extract('mydata')  # AssertionError: 只有zip/tar文件可以被解压缩\n# after\nDATA_HUB['mydata'] = (DATA_URL + 'mydata.zip', sha1_zip)\nd2l.download_extract('mydata')  # or simply d2l.download('mydata') for the csv","handlingStrategy":"validation","validationCode":"import os\nname = 'mydata'\next = os.path.splitext(d2l.DATA_HUB[name][0].split('/')[-1])[1]\nif ext not in ('.zip', '.tar', '.gz'):\n    raise ValueError(f'{name} is {ext}; download_extract supports zip/tar/gz only')\npath = d2l.download_extract(name)","typeGuard":"def is_extractable(name: str, hub=d2l.DATA_HUB) -> bool:\n    return os.path.splitext(hub[name][0].split('/')[-1])[1] in ('.zip', '.tar', '.gz')","tryCatchPattern":null,"preventionTips":["Check the URL's file extension before adding entries to DATA_HUB","Store plain files (csv/json) and call download() instead of download_extract()","Package custom datasets as .zip or .tar.gz","Verify archive type with `file` when filenames are unreliable"],"tags":["d2l","mxnet","assertion","archive","zip","tar","download"],"backgroundTag":null,"analyzedSha":"e6b18ccea71451a55fcd861d7b96fddf2587b09a","analyzedAt":"2026-08-14T20:05:26.414Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}