Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions fsspec/implementations/tests/test_zip.py
Original file line number Diff line number Diff line change
Expand Up @@ -607,6 +607,29 @@ def test_find_with_and_without_slash_should_return_same_result(zip_file):
assert zip_file_system.find("/dir2/") == zip_file_system.find("/dir2")


def test_find_prefix_respects_component_boundaries_and_separator_aliases():
files = {
"part/train/a.jsonl": b"a",
"part/train/deeper/b.jsonl": b"b",
"part/training/c.jsonl": b"c",
"part/../keep.jsonl": b"d",
}
with tempzip(files) as z:
fs = ZipFileSystem(z)
try:
assert fs.find("part\\train\\", maxdepth=1) == ["part/train/a.jsonl"]
result = fs.find("zip:///part//./train", detail=True, withdirs=True)
assert set(result) == {
"part/train",
"part/train/a.jsonl",
"part/train/deeper",
"part/train/deeper/b.jsonl",
}
assert fs.find("part/..") == ["part/../keep.jsonl"]
finally:
fs.close()


@pytest.mark.parametrize("prefix", ["/", "zip://", "zip:///"])
def test_find_should_return_file_if_exact_match(zip_file, prefix):
zip_file_system = ZipFileSystem(zip_file)
Expand Down
21 changes: 12 additions & 9 deletions fsspec/implementations/zip.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,15 +175,17 @@ def find(self, path, maxdepth=None, withdirs=False, detail=False, **kwargs):
if maxdepth is not None and maxdepth < 1:
raise ValueError("maxdepth must be at least 1")

def to_parts(_path: str):
return list(filter(None, _path.replace("\\", "/").split("/")))
def find_path(_path: str):
# Preserve the separator aliases used by find without changing
# the original cache keys returned to callers.
return "/".join(filter(None, _path.replace("\\", "/").split("/")))

if not isinstance(path, str):
path = str(path)

path = self._strip_protocol(path)
path_parts = to_parts(path)
path_depth = len(path_parts)
normalized = find_path(path)
prefix = normalized + "/"

self._get_dirs()

Expand All @@ -195,11 +197,11 @@ def to_parts(_path: str):
return result if detail else [path]

for file_path, file_info in self.dir_cache.items():
if len(file_parts := to_parts(file_path)) < path_depth or any(
a != b for a, b in zip(path_parts, file_parts)
):
# skip parent folders and mismatching paths
continue
if normalized:
# Ordinary cache keys are already normalized by _get_dirs().
candidate = find_path(file_path) if "\\" in file_path else file_path
if candidate != normalized and not candidate.startswith(prefix):
continue

if file_info["type"] == "directory":
if withdirs and file_path not in result:
Expand All @@ -210,6 +212,7 @@ def to_parts(_path: str):
result[file_path] = file_info if detail else None

if maxdepth:
path_depth = normalized.count("/") + bool(normalized)
result = {
k: v for k, v in result.items() if k.count("/") < maxdepth + path_depth
}
Expand Down
Loading