Search code examples
pythondownloadrequestzippython-zipfile

How to download files directly into an archive?


I want to download multiple files and save them in a zipfile.

Here is my code so far:

import zipfile
import requests
import os

pics = ['url/1.jpg', 'url/2.jpg', 'url/3.jpg']
dir = '/directory' 

with zipfile.ZipFile(dir + '/test.zip', 'w') as my_zip:
    for x in range(len(pics)):
        fn = dir + '/' + pics[x].split('/')[-1]
        
        r = requests.get(pics[x], allow_redirects=True)
        open(fn, 'wb').write(r.content)
    
        my_zip.write(fn, str(x+1) + os.path.splitext(fn)[1])
        
        os.remove(fn)

Is there a smarter/cleaner/shorter way to do this. Downloading the img, then putting it into the zip and then deleting it seems unnecessarily complicated.


Solution

  • I think you should you a temporary file in such cases.

    This should work:

    import requests
    import zipfile
    import tempfile
    
    pics = ['http://www.princeton.edu/~dancexp/Images/1-XPAlifun.jpg',
            'http://www.princeton.edu/~dancexp/Images/1-ExposeJess.jpg',
            'http://www.princeton.edu/~dancexp/Images/JessHsurevisedlores.jpg']
    
    picdirname = "mypics"
    
    with zipfile.ZipFile("test.zip", "w") as my_zip:
                         
        for pic in pics:
            response = requests.get(pic)
            if response.status_code == 200:
                tmpf = tempfile.TemporaryFile()
                tmpf.write(response.content)
                tmpf.seek(0)
                my_zip.writestr(pic.split('/')[-1], tmpf.read())
                tmpf.close()