Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support nested archives #19

Open
moi90 opened this issue Sep 30, 2022 · 1 comment
Open

Support nested archives #19

moi90 opened this issue Sep 30, 2022 · 1 comment

Comments

@moi90
Copy link

moi90 commented Sep 30, 2022

Currently, it is impossible to access nested archives:

from pathlib import Path
import zipfile
from archive_path.zip_path import ZipPath


def test_archive_path(tmp_path: Path):

    archive_dir = tmp_path / "path/to"
    archive_dir.mkdir(parents=True)

    archive_path = archive_dir / "archive.zip"

    with zipfile.ZipFile(archive_path, "w") as zf1:
        with zf1.open("nested.zip", "w") as f:
            with zipfile.ZipFile(f, "w") as zf2:
                zf2.writestr("hello.txt", "Hello World!")

    print()

    with ZipPath(archive_path) as zp1:
        with ZipPath(zp1 / "nested.zip") as zp2:
            # Should be path/to/archive.zip/nested.zip
            print("zp2:", repr(zp2))

            # Fails: FileNotFoundError
            zp2.read_text("hello.txt")

zp2 is ZipPath('.../path/to/archive.zip', '').
This is because of reuse of a parent archive in the constructor.

Instead, I suggest to use a private parameter to ZipPath.__init__ to pass along a shared _zipfile, so that we do not need path for that. Furthermore, ZipFileExtra should use Path.open instead of io.open so that path instances of any kind (e.g. ZipPath) can be used.

@moi90
Copy link
Author

moi90 commented Sep 30, 2022

Context: I'm working on a universal Path implementation that allows me to work on paths like "/path/to/archive.zip/nested.zip/hello.txt" transparently.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant