3

When I want to decompression a zipfile from internet, there is a error like zipfile.badzipfile: file is not a zip file. here is my code:

response = requests.get('www.example.com/somefile.zip', stream=True)
response.raw.decode_content = True
with zipfile.ZipFile(response.raw)as zip_file:
    # do something.

PS: Because this zipfile is very large, I don't want to waste memory, so I didn't use io.BytesIO(resp.content) as there 1 says.

Dharman
  • 30,962
  • 25
  • 85
  • 135
neo zoro
  • 31
  • 2
  • 2
    A streaming object will result in a partially complete file, which wouldn't work for a zipfile. You may get lucky by downloading in the exact right blocks for a zipfile, but that is unlikely. – 9769953 Aug 04 '21 at 02:25
  • @9769953 - would this work for compressed tar files? – jkr Aug 04 '21 at 12:15
  • I don't know. You would need to look up the details of tar files and how e.g., Python handles them. – 9769953 Aug 04 '21 at 15:42
  • @jakub, it should work. Gzip and Tar are both “streamable”; in Unix you can do something like `cat file.tar.gz | gzip -dc | tar -x -f -`. Tar stands for Tape ARchive, and was intended to extract or add files as the tape ran. – arielCo Nov 24 '21 at 03:25

0 Answers0