const axios = require('axios'); const fs = require('fs'); const { pipeline } = require('stream/promises'); async function downloadFile(url, outputPath) { const response = await axios({ url, method: 'GET', responseType: 'stream', }); await pipeline(response.data, fs.createWriteStream(outputPath)); console.log('Download complete!'); } Use code with caution. 2. Unzipping the File
Once the file is on your disk, you need a library to handle the .zip format, as Node's built-in zlib primarily handles single-file compression like Gzip. Option A: Adm-Zip (Simple & Synchronous) node.js axios download file stream and writeFile node.js download and unzip file
For modern environments (Node.js 18+), the native fetch() is often the simplest choice. If you prefer a more feature-rich ecosystem with automatic error handling for non-2xx status codes, Axios is a robust alternative. const axios = require('axios'); const fs = require('fs');
In modern Node.js development, downloading and unzipping files is a standard task for automation, updating assets, or processing data dumps. You can achieve this using the built-in Fetch API or libraries like Axios , combined with powerful decompression packages. 1. Downloading the Zip File Option A: Adm-Zip (Simple & Synchronous) node
Streaming is essential for large files to keep memory usage low. You can use stream.pipeline to safely write the download to your disk. javascript