class MyZipStore(zarr.ZipStore):
def __init__(self, path, compression=zipfile.ZIP_STORED, allowZip64=True, mode='a',
dimension_separator=None):
# store properties
if isinstance(path, str): # this is the only change needed to make this work
path = os.path.abspath(path)
self.path = path
self.compression = compression
self.allowZip64 = allowZip64
self.mode = mode
self._dimension_separator = dimension_separator
# Current understanding is that zipfile module in stdlib is not thread-safe,
# and so locking is required for both read and write. However, this has not
# been investigated in detail, perhaps no lock is needed if mode='r'.
self.mutex = RLock()
# open zip file
self.zf = zipfile.ZipFile(path, mode=mode, compression=compression,
allowZip64=allowZip64)
然后,您可以在内存中创建zip文件:
zip_buffer = io.BytesIO()
store = MyZipStore(zip_buffer)
ds.to_zarr(store)
1条答案
按热度按时间mf98qq941#
Zarr支持多个storage backends(DirectoryStore、ZipStore等),如果你正在寻找单个文件对象,听起来ZipStore就是你想要的。
zip文件可以看作是一个单独的文件zarr存储,可以下载(或作为一个单独的存储移动)。
更新1
如果你想在内存中完成所有这些操作,你可以扩展
zarr.ZipStore
以允许传入一个BytesIO对象:然后,您可以在内存中创建zip文件:
您会注意到
zip_buffer
包含一个有效的zip文件:(
PK\x03\x04
为Zip file magic number)