Skip to content
Advertisement

Zipfile: How to download a zipfile and save without extract?

I would like to download a zipfile from url and save somewhere. I don’t want to extract my file and that is my problem. I have a code which download zipfile and extract files, but I want to only download and save. What should I change?

from urllib.request import urlopen
from zipfile import ZipFile


  zipurl = 'https://opendata.geoportal.gov.pl/prg/adresy/PunktyAdresowe/POLSKA.zip'
    with urlopen(zipurl) as zipresp:
        with ZipFile(BytesIO(zipresp.read())) as zfile:
            zfile.extractall(r'/home/gis/adresypolska')  




Error:

Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
  File "/usr/lib/python3.8/http/client.py", line 471, in read
    s = self._safe_read(self.length)
  File "/usr/lib/python3.8/http/client.py", line 612, in _safe_read
    data = self.fp.read(amt)
  File "/usr/lib/python3.8/socket.py", line 669, in readinto
    return self._sock.recv_into(b)
  File "/usr/lib/python3.8/ssl.py", line 1241, in recv_into
    return self.read(nbytes, buffer)
  File "/usr/lib/python3.8/ssl.py", line 1099, in read
    return self._sslobj.read(len, buffer)
OverflowError: signed integer is greater than maximum

Advertisement

Answer

Just don’t use ZipFile at all. You have the file content, write it out:

zipurl = 'https://opendata.geoportal.gov.pl/prg/adresy/PunktyAdresowe/POLSKA.zip'
with open('POLSKA.zip', 'wb') as f:
    f.write(urlopen(zipurl).read())

To read and save in chunks, in case you have small RAM:

with open('POLSKA.zip', 'wb') as f:
    with urlopen(zipurl) as zipresp:
        while True:
            chunk = zipresp.read(1024)
            if not chunk: break
            f.write(chunk)

This reads 1024 bytes every iteration, you should change it accordingly to your need.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement