question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ZeroDivisionError in the end of zip file

See original GitHub issue

Thanks for the lib. Got ZeroDivisionError: integer division or modulo by zero during processing zip file’s last chunk from example code snippet:

Traceback (most recent call last):
  File "***\tmp.py", line 9, in <module>
    for file_name, file_size, unzipped_chunks in stream_unzip(zipped_chunks()):
  File "***\venv_win32_39py\lib\site-packages\stream_unzip.py", line 180, in stream_unzip
    for _ in yield_all():
  File "***\venv_win32_39py\lib\site-packages\stream_unzip.py", line 35, in _yield_all
    offset = (offset + to_yield) % len(chunk)
ZeroDivisionError: integer division or modulo by zero

Code snippet:

import httpx
from stream_unzip import stream_unzip


def zipped_chunks():
    # Any iterable that yields a zip file
    with httpx.stream('GET', 'https://www.gyan.dev/ffmpeg/builds/packages/ffmpeg-4.4-essentials_build.zip') as r:
        yield from r.iter_bytes()


for file_name, file_size, unzipped_chunks in stream_unzip(zipped_chunks()):
    for chunk in unzipped_chunks:
        # print(chunk)
        print(file_name, file_size)

Python 3.9.5 (Windows 10) stream-unzip 0.0.23

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
michalccommented, Jul 10, 2021

I’ve opted to not change the code, and instead change the README to have an example that works, and explicitly state that zero length chunks are not supported.

It’s a tricky call to make, but all things being equal, I’m happier with the error since it indicates something is unexpected earlier in the processing.

[If users do want to not fail with zero-length chunks, then they can filter them out as in the example above]

0reactions
michalccommented, Jul 4, 2021

Ah found a more robust workaround: specifying chunk_size in the call to iter_bytes makes it avoid the zero length chunk that stream-unzip doesn’t handle:

import httpx
from stream_unzip import stream_unzip

def zipped_chunks():
    with httpx.stream('GET', 'https://www.gyan.dev/ffmpeg/builds/packages/ffmpeg-4.4-essentials_build.zip') as r:
        yield from r.iter_bytes(chunk_size=65536)

for file_name, file_size, unzipped_chunks in stream_unzip(zipped_chunks()):
    for chunk in unzipped_chunks:
        print(file_name, file_size)
Read more comments on GitHub >

github_iconTop Results From Across the Web

division by zero occurs while requesting NHL data and writing ...
1 Answer 1 ... The reason why you ended up with the Divide-By-Zero error is because in this case, the item['goalsAgainst'] in Line...
Read more >
ZeroDivisionError division by zero in Python error handling
If you are working in Python, and receive the following output, your code is attempting to divide a given number by zero. ZeroDivisionError....
Read more >
ZeroDivisionError: float division - Support - Brian simulator
py extension and treat it as a normal zipped file. I believe the error lies in the differential equations (i.e. HH_equations.py) for the...
Read more >
Python Exception Handling - Try/Except Block, Finally Block
ZeroDivisionError: division by zero. During handling of the above exception, another exception occurred: Output. Traceback (most recent call last):File ...
Read more >
How to fix Python KeyError Exceptions in simple steps?
In this example, the zipfile.ZipFile class is used to derive information about a ZIP archive 'Batman' using the getinfo() function.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found