Download Returned Zip File from Url

Download Returned Zip file from URL

Most people recommend using requests if it is available, and the requests documentation recommends this for downloading and saving raw data from a url:

import requests 

def download_url(url, save_path, chunk_size=128):
r = requests.get(url, stream=True)
with open(save_path, 'wb') as fd:
for chunk in r.iter_content(chunk_size=chunk_size):
fd.write(chunk)

Since the answer asks about downloading and saving the zip file, I haven't gone into details regarding reading the zip file. See one of the many answers below for possibilities.

If for some reason you don't have access to requests, you can use urllib.request instead. It may not be quite as robust as the above.

import urllib.request

def download_url(url, save_path):
with urllib.request.urlopen(url) as dl_file:
with open(save_path, 'wb') as out_file:
out_file.write(dl_file.read())

Finally, if you are using Python 2 still, you can use urllib2.urlopen.

from contextlib import closing

def download_url(url, save_path):
with closing(urllib2.urlopen(url)) as dl_file:
with open(save_path, 'wb') as out_file:
out_file.write(dl_file.read())

How to download .zip file that i recieve from a HTTP response (axios PUT request)

I would suggest fetch over axios in concern of zip file because sometimes the response in axios request is not accurate and you might fall into currupted zip file while downloading, the best might be using a package called file-saver and fetch. I am posting this answer to help the developers to grab the concept only, the code is tested in React.

package file-saver:https://www.npmjs.com/package/file-saver

Now make any function according to your choice in react, I am assuming functional component so will write method according to functional component syntax.
note before using saveAs function you need to import from the installed package file-saver.

import { saveAs } from 'file-saver';

const downloadZipFileFromLaravel=()=>{
fetch(`your url`)

.then(res => res.blob())
.then(blob => saveAs(blob, 'Auto Photos.zip')) // saveAs is a function from the file-saver package.
.catch((err) => {
console.log(err.message);
});
}

at the end you need to connect the function with a button with onClick.
example

<button onClick={()=>downloadZipFileFromLaravel()}> </button>

Note: usage of file saver in pure javascript, you can check this:
How to use filesaver.js

For more information you can see the below discussion:
Reference: https://github.com/eligrey/FileSaver.js/issues/156

Android: Download Returned Zip file from URL

public void downloadFromUrl(String url, String outputFileName) {

File dir = new File(Environment.getExternalStorageDirectory() + "/");
if (dir.exists() == false) {
dir.mkdirs();
}
try {
URL downloadUrl = new URL(url); // you can write any link here

File file = new File(dir, outputFileName);
/* Open a connection to that URL. */
URLConnection ucon = downloadUrl.openConnection();

/*
* Define InputStreams to read from the URLConnection.
*/
InputStream is = ucon.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);

/*
* Read bytes to the Buffer until there is nothing more to read(-1).
*/
ByteArrayBuffer baf = new ByteArrayBuffer(5000);
int current = 0;
while ((current = bis.read()) != -1) {
baf.append((byte) current);
}

/* Convert the Bytes read to a String. */
FileOutputStream fos = new FileOutputStream(file);
fos.write(baf.toByteArray());
fos.flush();
fos.close();

} catch (IOException e) {
e.printStackTrace();

}

}

Downloading zip files from a pop-up web link

I have found a solution to the problem. This does not even need the zip file module. It downloads the contents and then it puts them into the archive.

import requests

url = "http://"+"www.sec.gov/dera/data/Public-EDGAR-log-file-data/2003/Qtr4/log20031231.zip"
open_url = requests.get(url)

with open('log20031231_2.zip', "wb") as code:
code.write(open_url.content)

How to bypass the alert window while downloading a zipfile?

I believe you are accepting answer using selenium, Here's what you can do using selenium :

from selenium import webdriver
from selenium.webdriver.firefox.options import Options

profile = webdriver.FirefoxProfile()

profile.set_preference("browser.download.folderList",1)
# 0 for desktop
# 1 for default download folder
# 2 for specific folder
# You can specify directory by using profile.set_preference("browser.download.dir","<>")

profile.set_preference("browser.helperApps.neverAsk.saveToDisk", "application/octet-stream")
profile.set_preference("browser.helperApps.alwaysAsk.force", False);

# If you don't have some download manager then you can remove these
profile.set_preference("browser.download.manager.showWhenStarting",False)
profile.set_preference("browser.download.manager.useWindow", False);
profile.set_preference("browser.download.manager.focusWhenStarting", False);
profile.set_preference("browser.download.manager.alertOnEXEOpen", False);
profile.set_preference("browser.download.manager.showAlertOnComplete", False);

driver=webdriver.Firefox(firefox_profile=profile,executable_path="<>")

driver.get("https://dibbs2.bsm.dla.mil/Downloads/RFQ/Archive/ca210731.zip")
driver.find_element_by_id("butAgree").click()

Here we are setting some profiles to disable pop out, download dialog.

It is working perfectly fine in latest version of Firefox and 3.141.0 version of selenium



Related Topics



Leave a reply



Submit