Skip to content

Commit 1823a09

Browse files
ilan-golddcherian
andauthored
(fix): ensure zip directory store compares key to prefix correctly (#2758)
* (fix): ensure zip directory store compares key to prefix correctly * (chore): add test for externally zipped zarr store * (fix): no need for async test * (chore): rel note --------- Co-authored-by: Deepak Cherian <[email protected]>
1 parent 037adf6 commit 1823a09

File tree

3 files changed

+16
-1
lines changed

3 files changed

+16
-1
lines changed

changes/2758.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix zip-store path checking for stores with directories listed as files.

src/zarr/storage/_zip.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ async def list_dir(self, prefix: str) -> AsyncIterator[str]:
283283
yield key
284284
else:
285285
for key in keys:
286-
if key.startswith(prefix + "/") and key != prefix:
286+
if key.startswith(prefix + "/") and key.strip("/") != prefix:
287287
k = key.removeprefix(prefix + "/").split("/")[0]
288288
if k not in seen:
289289
seen.add(k)

tests/test_store/test_zip.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from __future__ import annotations
22

33
import os
4+
import shutil
45
import tempfile
56
import zipfile
67
from typing import TYPE_CHECKING
@@ -14,6 +15,7 @@
1415
from zarr.testing.store import StoreTests
1516

1617
if TYPE_CHECKING:
18+
from pathlib import Path
1719
from typing import Any
1820

1921

@@ -111,3 +113,15 @@ async def test_zip_open_mode_translation(
111113
kws = {**store_kwargs, "mode": zip_mode}
112114
store = await self.store_cls.open(**kws)
113115
assert store.read_only == read_only
116+
117+
def test_externally_zipped_store(self, tmp_path: Path) -> None:
118+
# See: https://github.com/zarr-developers/zarr-python/issues/2757
119+
zarr_path = tmp_path / "foo.zarr"
120+
root = zarr.open_group(store=zarr_path, mode="w")
121+
root.require_group("foo")
122+
root["foo"]["bar"] = np.array([1])
123+
shutil.make_archive(zarr_path, "zip", zarr_path)
124+
zip_path = tmp_path / "foo.zarr.zip"
125+
zipped = zarr.open_group(ZipStore(zip_path, mode="r"), mode="r")
126+
assert list(zipped.keys()) == list(root.keys())
127+
assert list(zipped["foo"].keys()) == list(root["foo"].keys())

0 commit comments

Comments
 (0)