'Python : extract files from zipfile from web without downloading and saving it first

import re
import requests
import zipfile
import werkzeug
werkzeug.cached_property = werkzeug.utils.cached_property

from robobrowser import RoboBrowser

br = RoboBrowser(history=True)
br.open("loginurl")
forms = br.get_forms()

form=forms[0]

form['username'] = 'myname'
form['password'] = 'mypass'
br.submit_form(form)

url = "ulrlocationofzipfiledownload"
request = br.session.get(url, stream=True)

with open ('data.zip', 'wb') as f:
           f.write(request.content)

As of now, this works and it downloads a zipfile (to my desktop where the script is located)

I want it to extract specific contents from the zipfile to a specific location without creating the zipfile on my desktop

I tried some variations of this code:

with zipfile.ZipFile('files.zip','r') as f:
      myzipfile.extractall('files')

but I couldn't get it to work.

Is it possible to get zipfile from the web and extract some of its specific contents to a specific locations on PC, all without saving the downloaded zipfile first (maybe only keeping the zipfile "open" in RAM ?)



Solution 1:[1]

You can avoid to actually save the file, but using requests

import requests, zipfile, io

url = 'url-to-zipfile'

response = requests.get(url, stream=True)
zipfile = zipfile.ZipFile(io.BytesIO(response.content))

zipfile.extractall()

I'm not sure RoboBrowser is able to do that.

Solution 2:[2]

I made it :)

import requests
import zipfile
import werkzeug
import io
import subprocess

werkzeug.cached_property = werkzeug.utils.cached_property

from robobrowser import RoboBrowser

br = RoboBrowser(history=True)
br.open("loginurlhere")
forms = br.get_forms()

form=forms[0]   #when there are multiple forms in website, which form to select? (0=1st)

form['username'] = 'myusername'
form['password'] = 'mypass'
br.submit_form(form)

url1 = "DirectUrlof1stFileToDownload"

url2 = "DirectUrlof2ndFileToDownload"

# D:\Games\..\...  represent an example of location where to extract the file(s)
response1 = br.session.get(url1, stream=True)
zipfile1 = zipfile.ZipFile(io.BytesIO(response1.content))
zipfile1.extract('nameOftheFileInsideZipToExtract','D:\Games\..\...')

response2 = br.session.get(url2, stream=True)
zipfile2 = zipfile.ZipFile(io.BytesIO(response2.content))
zipfile2.extract('nameOftheFileInsideZipToExtract','D:\Games\..\...')
zipfile2.extract('nameOftheFileInsideZipToExtract','D:\Games\..\...')
zipfile2.extract('nameOftheFileInsideZipToExtract','D:\Games\..\...')


#For executing a specific file (I use it to execute an extracted file)
subprocess.call([r'D:\Games\..\...'])



''' For saving a zip file
request = br.session.get(url1, stream=True)
with open ('data.zip', 'wb') as f:
           f.write(request.content)
'''

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Arount
Solution 2 mato200