Extract archive file. Parameters ---------- path: Path of archive file to be extracted. to: Directory to which the archive file will be extracted. If None, it will be set to the parent directory of the archive file. Raises ------ ValueError
(path: str, to: str | None = None)
| 15 | |
| 16 | |
| 17 | def extractall(path: str, to: str | None = None) -> list[str]: |
| 18 | """Extract archive file. |
| 19 | |
| 20 | Parameters |
| 21 | ---------- |
| 22 | path: |
| 23 | Path of archive file to be extracted. |
| 24 | to: |
| 25 | Directory to which the archive file will be extracted. |
| 26 | If None, it will be set to the parent directory of the archive file. |
| 27 | |
| 28 | Raises |
| 29 | ------ |
| 30 | ValueError |
| 31 | If the archive format is unsupported, or if an archive member would |
| 32 | extract outside the target directory. |
| 33 | """ |
| 34 | if to is None: |
| 35 | to = osp.dirname(path) |
| 36 | |
| 37 | if path.endswith(".zip"): |
| 38 | return _extractall_zip(path=path, to=to) |
| 39 | |
| 40 | if path.endswith(".tar"): |
| 41 | tar_mode = "r" |
| 42 | elif path.endswith(".tar.gz") or path.endswith(".tgz"): |
| 43 | tar_mode = "r:gz" |
| 44 | elif path.endswith(".tar.bz2") or path.endswith(".tbz"): |
| 45 | tar_mode = "r:bz2" |
| 46 | else: |
| 47 | raise ValueError( |
| 48 | f"Could not extract '{path}' as no appropriate extractor is found" |
| 49 | ) |
| 50 | |
| 51 | return _extractall_tar(path=path, to=to, tar_mode=tar_mode) |
| 52 | |
| 53 | |
| 54 | def _extractall_zip(path: str, to: str) -> list[str]: |
searching dependent graphs…