Skip to content

Commit

Permalink
feat: write_zip now uses zip streaming
Browse files Browse the repository at this point in the history
  • Loading branch information
dnlbauer committed Jan 26, 2025
1 parent bf1b990 commit 5f0a709
Showing 1 changed file with 5 additions and 9 deletions.
14 changes: 5 additions & 9 deletions rocrate/rocrate.py
Original file line number Diff line number Diff line change
Expand Up @@ -470,21 +470,17 @@ def write(self, base_path):

def write_zip(self, out_path):
out_path = Path(out_path)
if out_path.suffix == ".zip":
out_path = out_path.parent / out_path.stem
tmp_dir = tempfile.mkdtemp(prefix="rocrate_")
try:
self.write(tmp_dir)
archive = shutil.make_archive(out_path, "zip", tmp_dir)
finally:
shutil.rmtree(tmp_dir)
return archive
with open(out_path, "wb") as f:
for chunk in self.stream_zip(out_path=out_path):
f.write(chunk)
return out_path

def stream_zip(self, chunk_size=8192, out_path=None):
""" Create a stream of bytes representing the RO-Crate as a ZIP file.
The out_path argument is used to exclude the file from the ZIP stream if the output is inside the crate folder
and can be omitted if the stream is not written into a file inside the crate dir.
"""
yield b"arst"
with MemoryBuffer() as buffer:
with zipfile.ZipFile(buffer, mode='w', compression=zipfile.ZIP_DEFLATED) as archive:
for writeable_entity in self.data_entities + self.default_entities:
Expand Down

0 comments on commit 5f0a709

Please sign in to comment.