{"record":{"id":"cd2bfaab88d697ce","repo":"d2l-ai/d2l-zh","slug":"zip-tar-cd2bfa","errorCode":null,"errorMessage":"只有zip/tar文件可以被解压缩","messagePattern":"只有zip/tar文件可以被解压缩","errorType":"validation","errorClass":"AssertionError","httpStatus":null,"severity":"error","filePath":"d2l/tensorflow.py","lineNumber":395,"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":377,"sourceCodeEnd":413,"githubUrl":"https://github.com/d2l-ai/d2l-zh/blob/e6b18ccea71451a55fcd861d7b96fddf2587b09a/d2l/tensorflow.py#L377-L413","documentation":"An AssertionError (assert False) in d2l.tensorflow.download_extract that fires when the downloaded file's extension is neither .zip nor .tar/.gz. The function only knows how to open archives via zipfile.ZipFile or tarfile.open; any other extension aborts with the Chinese message 'only zip/tar files can be extracted'.","triggerScenarios":"Registering a DATA_HUB entry whose URL ends in .csv, .json, .txt, .pt, or .7z and then calling download_extract on it; a URL with no filename extension at all (query-string URLs); double-extension files where os.path.splitext picks up the wrong part (e.g. 'data.tar.bz2' yields '.bz2').","commonSituations":"Users pointing DATA_HUB at raw CSVs (correct target is download(), not download_extract()); .bz2 or .xz compressed archives from non-D2L sources; URLs like https://host/file?format=zip whose splitext result is ''. ","solutions":["For plain files (csv/json/txt), call d2l.download(name) instead of download_extract(name) — no extraction is needed.","For .bz2/.xz archives, pre-extract manually (tar xjf file.tar.bz2) or register a .zip/.tar.gz mirror in DATA_HUB.","Fix the registered URL so it ends with a real archive extension before calling download_extract.","For .tar.bz2 specifically, note os.path.splitext returns '.bz2'; rename or handle with tarfile.open(fname, 'r:bz2') in your own code."],"exampleFix":"# before\nd2l.DATA_HUB['raw_csv'] = (d2l.DATA_URL + 'data.csv', sha1)\nfname = d2l.download_extract('raw_csv')  # AssertionError: csv is not zip/tar\n# after\nfname = d2l.download('raw_csv')  # plain download, no extraction","handlingStrategy":"validation","validationCode":"import os\n_, ext = os.path.splitext(fname)\nif ext in ('.zip', '.tar', '.gz'):\n    out = d2l.download_extract(name)\nelif ext in ('.csv', '.json', '.txt'):\n    out = d2l.download(name)\nelse:\n    raise ValueError(f'unsupported extension {ext!r}; extract {fname} manually')","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)  # plain file, no extraction needed\n    else:\n        raise","preventionTips":["Use download() for plain files and download_extract() only for .zip/.tar/.gz.","Remember splitext('a.tar.bz2') -> '.bz2' is rejected; pre-extract such archives yourself.","Register DATA_HUB URLs that end in a real, recognizable extension."],"tags":["d2l","tensorflow","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"}