{"record":{"id":"d79d4f794ea8dfc5","repo":"d2l-ai/d2l-zh","slug":"zip-tar-d79d4f","errorCode":null,"errorMessage":"只有zip/tar文件可以被解压缩","messagePattern":"只有zip/tar文件可以被解压缩","errorType":"validation","errorClass":"AssertionError","httpStatus":null,"severity":"error","filePath":"d2l/paddle.py","lineNumber":417,"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":399,"sourceCodeEnd":435,"githubUrl":"https://github.com/d2l-ai/d2l-zh/blob/e6b18ccea71451a55fcd861d7b96fddf2587b09a/d2l/paddle.py#L399-L435","documentation":"An AssertionError (assert False) in d2l.paddle.download_extract raised when the downloaded file's extension is not .zip, .tar, or .gz. The helper can only extract archives via zipfile.ZipFile or tarfile.open; the Chinese message translates to 'only zip/tar files can be extracted'.","triggerScenarios":"Calling download_extract on a DATA_HUB entry whose URL ends in .csv/.json/.txt; extensionless or query-string URLs; .bz2/.xz archives where os.path.splitext yields an unrecognized extension.","commonSituations":"Registering raw CSVs (the Kaggle house files) and mistakenly using download_extract instead of download; pointing DATA_HUB at third-party archives in formats the helper does not handle.","solutions":["Use d2l.download(name) for non-archive files — extraction is not needed.","Register/point to .zip or .tar.gz mirrors when you truly need extraction.","Handle .bz2/.xz yourself with tarfile.open(fname, 'r:bz2'|'r:xz') in custom code.","Verify the extension logic: os.path.splitext('a.tar.gz') returns '.gz' (handled), but 'a.csv' returns '.csv' (rejected)."],"exampleFix":"# before\nd2l.DATA_HUB['house'] = (d2l.DATA_URL + 'kaggle_house_pred_train.csv', sha1)\nd2l.download_extract('house')  # AssertionError: csv not extractable\n# after\nd2l.download('house')  # returns path to the csv directly","handlingStrategy":"validation","validationCode":"_, ext = os.path.splitext(fname)\nif ext in ('.zip', '.tar', '.gz'):\n    out = d2l.download_extract(name)\nelse:\n    out = d2l.download(name)  # plain file needs no extraction","typeGuard":"def is_extractable(fname: str) -> bool:\n    return os.path.splitext(fname)[1] in ('.zip', '.tar', '.gz')","tryCatchPattern":"try:\n    d2l.download_extract(name)\nexcept AssertionError as e:\n    if 'zip/tar' in str(e):\n        path = d2l.download(name)\n    else:\n        raise","preventionTips":["Reserve download_extract for archives; use download for CSVs and other raw files.","Watch for .bz2/.xz archives — pre-extract them manually.","Register URLs ending in a clean extension."],"tags":["d2l","paddle","download","archive","zip","tar","assertion"],"backgroundTag":null,"analyzedSha":"e6b18ccea71451a55fcd861d7b96fddf2587b09a","analyzedAt":"2026-08-14T20:05:26.414Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}