{"record":{"id":"3fd08aa178b0705b","repo":"ansible/ansible","slug":"unable-to-list-files-in-the-archive","errorCode":null,"errorMessage":"Unable to list files in the archive","messagePattern":"Unable to list files in the archive","errorType":"exception","errorClass":"UnarchiveError","httpStatus":null,"severity":"error","filePath":"lib/ansible/modules/unarchive.py","lineNumber":385,"sourceCode":"        if self._infodict:\n            return self._infodict[path]\n\n        try:\n            archive = ZipFile(self.src)\n        except BadZipFile as e:\n            if e.args[0].lower().startswith('bad magic number'):\n                # Python2.4 can't handle zipfiles with > 64K files.  Try using\n                # /usr/bin/unzip instead\n                self._legacy_file_list()\n            else:\n                raise\n        else:\n            try:\n                for item in archive.infolist():\n                    self._infodict[item.filename] = int(item.CRC)\n            except Exception:\n                archive.close()\n                raise UnarchiveError('Unable to list files in the archive')\n\n        return self._infodict[path]\n\n    @property\n    def files_in_archive(self):\n        if self._files_in_archive:\n            return self._files_in_archive\n\n        self._files_in_archive = []\n        try:\n            archive = ZipFile(self.src)\n        except BadZipFile as e:\n            if e.args[0].lower().startswith('bad magic number'):\n                # Python2.4 can't handle zipfiles with > 64K files.  Try using\n                # /usr/bin/unzip instead\n                self._legacy_file_list()\n            else:\n                raise","sourceCodeStart":367,"sourceCodeEnd":403,"githubUrl":"https://github.com/ansible/ansible/blob/9cf16a4aca7898481c257f1e17ad28d0b67b1f85/lib/ansible/modules/unarchive.py#L367-L403","documentation":"While computing CRCs, unarchive iterates zipfile.infolist(); any unexpected Exception during that listing is converted to UnarchiveError('Unable to list files in the archive'). Unlike the BadZipFile path, the archive opened but its central directory is inconsistent enough that listing raised.","triggerScenarios":"A zip whose central directory references corrupt/oversized entries; a file that changes concurrently while being listed; a crafted zip triggering a Python zipfile internal error.","commonSituations":"Damaged downloads; archives produced by tools with nonstandard zip dialects; reading an archive still being written by another process.","solutions":["Test the archive independently: python3 -c \"import zipfile; zipfile.ZipFile('x.zip').infolist()\"","Re-download or re-create the archive (checksum it)","Regenerate the zip with a mainstream tool (standard zip) if the producer is exotic","Ensure no concurrent writer touches src during the task"],"exampleFix":"# before\n- ansible.builtin.unarchive:\n    src: /tmp/app.zip\n    dest: /opt/app\n    remote_src: true\n\n# after: pre-validate on target\n- name: validate zip\n  ansible.builtin.command: python3 -c \"import zipfile; zipfile.ZipFile('/tmp/app.zip').infolist()\"\n  changed_when: false\n- ansible.builtin.unarchive:\n    src: /tmp/app.zip\n    dest: /opt/app\n    remote_src: true","handlingStrategy":"try-catch","validationCode":"import zipfile\n\ndef zip_lists_cleanly(path):\n    try:\n        with zipfile.ZipFile(path) as z:\n            z.infolist()\n        return True\n    except Exception:\n        return False","typeGuard":null,"tryCatchPattern":"try:\n    crc = handler._crc32(path)\nexcept UnarchiveError as e:\n    module.fail_json(msg=str(e), hint='zip central directory unreadable; re-create archive')","preventionTips":["Regenerate zips with mainstream tools","Never read an archive while it is being written","Verify transfers end-to-end with checksums"],"tags":["unarchive","zip","corruption","ansible-module"],"backgroundTag":null,"analyzedSha":"9cf16a4aca7898481c257f1e17ad28d0b67b1f85","analyzedAt":"2026-08-15T00:15:47.100Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}