Skip to content

Commit 22ded1d

Browse files
Fix bug where the checksum of zipfiles is wrong (#930)
* Fix bug where the checksum of zipfiles is wrong This bug is caused by incorrect length being written to the file, because Zipfile thinks the len() of the passed object is the length in bytes, but it was passing a ndarray, whose len() is the number of rows. The fix is to convert to bytes before passing to zipfile.writestr() * add test and use a view instead of a copy Co-authored-by: jakirkham <[email protected]>
1 parent 094eee2 commit 22ded1d

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

zarr/storage.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1551,7 +1551,7 @@ def __getitem__(self, key):
15511551
def __setitem__(self, key, value):
15521552
if self.mode == 'r':
15531553
raise ReadOnlyError()
1554-
value = ensure_contiguous_ndarray(value)
1554+
value = ensure_contiguous_ndarray(value).view("u1")
15551555
with self.mutex:
15561556
# writestr(key, value) writes with default permissions from
15571557
# zipfile (600) that are too restrictive, build ZipInfo for

zarr/tests/test_storage.py

+7
Original file line numberDiff line numberDiff line change
@@ -1554,6 +1554,13 @@ def test_permissions(self):
15541554
assert perm == '0o40775'
15551555
z.close()
15561556

1557+
def test_store_and_retrieve_ndarray(self):
1558+
store = ZipStore('data/store.zip')
1559+
x = np.array([[1, 2], [3, 4]])
1560+
store['foo'] = x
1561+
y = np.frombuffer(store['foo'], dtype=x.dtype).reshape(x.shape)
1562+
assert np.array_equiv(y, x)
1563+
15571564

15581565
class TestDBMStore(StoreTests):
15591566

0 commit comments

Comments
 (0)