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?
JavaScript
x
28
28
1
from urllib.request import urlopen
2
from zipfile import ZipFile
3
4
5
zipurl = 'https://opendata.geoportal.gov.pl/prg/adresy/PunktyAdresowe/POLSKA.zip'
6
with urlopen(zipurl) as zipresp:
7
with ZipFile(BytesIO(zipresp.read())) as zfile:
8
zfile.extractall(r'/home/gis/adresypolska')
9
10
11
12
13
Error:
14
15
Traceback (most recent call last):
16
File "<stdin>", line 2, in <module>
17
File "/usr/lib/python3.8/http/client.py", line 471, in read
18
s = self._safe_read(self.length)
19
File "/usr/lib/python3.8/http/client.py", line 612, in _safe_read
20
data = self.fp.read(amt)
21
File "/usr/lib/python3.8/socket.py", line 669, in readinto
22
return self._sock.recv_into(b)
23
File "/usr/lib/python3.8/ssl.py", line 1241, in recv_into
24
return self.read(nbytes, buffer)
25
File "/usr/lib/python3.8/ssl.py", line 1099, in read
26
return self._sslobj.read(len, buffer)
27
OverflowError: signed integer is greater than maximum
28
Advertisement
Answer
Just don’t use ZipFile
at all. You have the file content, write it out:
JavaScript
1
4
1
zipurl = 'https://opendata.geoportal.gov.pl/prg/adresy/PunktyAdresowe/POLSKA.zip'
2
with open('POLSKA.zip', 'wb') as f:
3
f.write(urlopen(zipurl).read())
4
To read and save in chunks, in case you have small RAM:
JavaScript
1
7
1
with open('POLSKA.zip', 'wb') as f:
2
with urlopen(zipurl) as zipresp:
3
while True:
4
chunk = zipresp.read(1024)
5
if not chunk: break
6
f.write(chunk)
7
This reads 1024 bytes every iteration, you should change it accordingly to your need.