|
| 1 | +import zipfile |
1 | 2 | from pathlib import Path |
2 | 3 |
|
3 | 4 | import numpy as np |
4 | 5 | import pytest |
5 | 6 | import zarr |
6 | | -from zarr.storage import LocalStore, MemoryStore, StorePath |
| 7 | +from zarr.storage import LocalStore, MemoryStore, StorePath, ZipStore |
7 | 8 |
|
8 | 9 | from ome_zarr import OMEZarrImage, OMEZarrMultiscale |
9 | 10 | from ome_zarr.data import create_zarr |
@@ -117,3 +118,24 @@ def test_class_writer_on_a_store(): |
117 | 118 | zarr.open_group(store, path="images/img", mode="r") |
118 | 119 | ) |
119 | 120 | np.testing.assert_array_equal(np.asarray(read_back.images[0].data), IMAGE) |
| 121 | + |
| 122 | + |
| 123 | +def test_read_from_a_zip(tmp_path): |
| 124 | + """A zipped OME-Zarr reads through ZarrLocation on a ZipStore.""" |
| 125 | + # Dask writes to a ZipStore corrupt the archive (zarr-python#3516), |
| 126 | + # so the test zips a directory store instead of writing to the ZipStore. |
| 127 | + directory = tmp_path / "img.zarr" |
| 128 | + write_image(IMAGE, zarr.open_group(directory, mode="w"), axes="yx", scaler=None) |
| 129 | + archive = tmp_path / "img.zarr.zip" |
| 130 | + with zipfile.ZipFile(archive, mode="w") as zf: |
| 131 | + for file in directory.rglob("*"): |
| 132 | + if file.is_file(): |
| 133 | + zf.write(file, file.relative_to(directory).as_posix()) |
| 134 | + |
| 135 | + store = ZipStore(archive, mode="r") |
| 136 | + loc = ZarrLocation(store) |
| 137 | + assert loc.exists() |
| 138 | + nodes = list(Reader(loc)()) |
| 139 | + assert nodes, "the reader found no node in the archive" |
| 140 | + np.testing.assert_array_equal(np.asarray(nodes[0].data[0]), IMAGE) |
| 141 | + store.close() |
0 commit comments