I want to download a file that windows tells me is 76mb big (right click properties says 74mb). But when I download it with the code below, the file will be 139mb (properties says 142mb) big. It seems like the file gets saved two times resulting in a corrupted file.
const fs = require('fs');
const { http, https } = require('follow-redirects');
function download(url, callback) {
var request = https.get(url, function (response) {
var body = "";
response.on("data", function (chunk) {
body = chunk;
});
response.on("end", function () {
callback(body);
});
request.on("error", function (e) {
console.log("Error: " e.message);
});
});
};
let url = "https://github.com/TheOtherRolesAU/TheOtherRoles/releases/latest/download/TheOtherRoles.zip"
download(url, (body) => {
console.log(body.length)
fs.writeFile("test.zip", body, err => {
if (err) console.error(err)
console.log("Done");
})
})
Any idea how this can happen? Any no, download()
doesn't get called twice.
Edit:
I forgot to mention but I am downloading from github and there is a redirect at the url and I am using this package as https
: https://www.npmjs.com/package/follow-redirects
CodePudding user response:
The library works fine, it's not being saved twice: you're not processing incoming data correctly: it's not a String
, it's a Buffer
, so you need to push each chunk, and then merge them and pass them to writer:
var body = [];
response.on("data", function (chunk) {
body.push(chunk);
});
response.on("end", function () {
callback(Buffer.concat(body));
});
or you can simply pipe the response to writeStream
and replace all that code with one-liner:
const zip = fs.createWriteStream("test.zip");
//...
response.pipe(zip);