0

I am using Python version 3.9.5. I am writing a python script to download the zip file from the browser. here is the code. It works fine if I extract all the files from the zip file. But I just want to download the main zip file which contains all the inner files. How do I do that? Zipfile.write() did not work.

import requests
import os 
import json
from robot.api.deco import keyword
import zipfile, io


def download_file(url):
    headers=   {"Auth": "{abcd}", 
                "accept": "*/*",               
               "accept-encoding": "gzip;deflate;br" }
    response = requests.request("GET", url, headers = headers)
    
    
    filename = os.getcwd()+'/downloads/'
        
    z = zipfile.ZipFile(io.BytesIO(response.content))
    
    z.extractall(filename)            
    
    return response.status_code

This code works absolutely fine but I just want to download the main zip file and not extract files.

topcat
  • 586
  • 1
  • 6
  • 30
Jay
  • 339
  • 1
  • 7
  • 23

2 Answers2

0
# remove these 2 lines which extract zip
z = zipfile.ZipFile(io.BytesIO(response.content))
z.extractall(filename)

# add these saves zip file
with open(filename, "w") as f:
  f.write(response.content)

edited: thanks for with comments

Ruslan Tushov
  • 1,153
  • 1
  • 10
  • 11
  • Note that it's [recommended](https://docs.python.org/3/tutorial/inputoutput.html#reading-and-writing-files) with use the `with open()` context manager to automatically handle closing the file. Otherwise, make sure to assign the file object to a variable and then call its `.close()` method afterward. – CrazyChucky Sep 17 '21 at 14:49
  • It creates a zip file if I write code like this. open(filename+'download1.zip', 'wb').write(response.content) . the file is downloaded but if I try to extract files it gives error "Error: Central directory not found". – Jay Sep 17 '21 at 14:50
  • I tried it using the with open() option. It works but again I have the same issue. if I try to extract files it gives the error "Error: Central directory not found" in the file manager – Jay Sep 17 '21 at 14:52
  • I tried using zip file only because file.write() is not working as expected. – Jay Sep 17 '21 at 14:53
  • @Jay If that's the case, then honestly I would make that your question. You can still show your current method as an alternative that you tried and it "worked", so you at least know the website isn't just serving you a corrupted file. But it's not really what you're trying to do or ask about. – CrazyChucky Sep 17 '21 at 14:57
  • @Jay And this answer (as long as you close the file afterward) is what anyone would first suggest based on your question as currently presented, so we need to know from your question that you've already tried it, and how it's failing. – CrazyChucky Sep 17 '21 at 15:01
  • It does not work... – M. Mariscal Nov 22 '22 at 08:58
0

If you don't need to look inside the zip file, you don't need any zip-aware code to download it - you just want to save the response data to a file.

There's a good example here: https://stackoverflow.com/a/16696317/3811862.