Skip to content

Commit

Permalink
fix: empty ZIP file
Browse files Browse the repository at this point in the history
This fixes the issues raised in
#77 where a ZIP file that has no
members would raise

UnexpectedSignatureError: b'PK\x05\x06'

There was an assumption in the code that either there would be no bytes at all,
or there would be some bytes and at least one member.

(I'm not entirely sure that no bytes should be treated as a valid ZIP come to
think of it... but leaving that behaviour as it was)
  • Loading branch information
michalc committed Jan 26, 2024
1 parent 3d8a79e commit 7ee80a2
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions test.py
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,16 @@ def yield_input():

self.assertEqual(files, [(b'first.txt', 0, b'')])

def test_empty_zip(self):
def yield_input():
file = io.BytesIO()
with zipfile.ZipFile(file, 'w', zipfile.ZIP_DEFLATED) as zf:
pass

yield file.getvalue()

self.assertEqual(list(stream_unzip(yield_input())), [])

def test_not_zip(self):
with self.assertRaises(UnexpectedSignatureError):
next(stream_unzip([b'This is not a zip file']))
Expand Down

0 comments on commit 7ee80a2

Please sign in to comment.