Skip to content

Commit

Permalink
fix: make sure zip stream buffer is always properly closed
Browse files Browse the repository at this point in the history
  • Loading branch information
dnlbauer committed Jan 24, 2025
1 parent 27457d8 commit 0a1c4a1
Showing 1 changed file with 16 additions and 17 deletions.
33 changes: 16 additions & 17 deletions rocrate/rocrate.py
Original file line number Diff line number Diff line change
Expand Up @@ -482,23 +482,22 @@ def write_zip(self, out_path):

def stream_zip(self):
""" Create a stream of bytes representing the RO-Crate as a ZIP file. """
buffer = MemoryBuffer()
with zipfile.ZipFile(buffer, mode='w', compression=zipfile.ZIP_DEFLATED) as archive:
for writeable_entity in self.data_entities + self.default_entities:
current_file_path, current_out_file = None, None
for path, chunk in writeable_entity.stream():
if path != current_file_path:
if current_out_file:
current_out_file.close()
current_file_path = path
current_out_file = archive.open(path, mode='w')
current_out_file.write(chunk)
yield buffer.read()
if current_out_file:
current_out_file.close()

yield buffer.read()
buffer.close()
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:
current_file_path, current_out_file = None, None
for path, chunk in writeable_entity.stream():
if path != current_file_path:
if current_out_file:
current_out_file.close()
current_file_path = path
current_out_file = archive.open(path, mode='w')
current_out_file.write(chunk)
yield buffer.read()
if current_out_file:
current_out_file.close()

yield buffer.read()

def add_workflow(
self, source=None, dest_path=None, fetch_remote=False, validate_url=False, properties=None,
Expand Down

0 comments on commit 0a1c4a1

Please sign in to comment.