ftp.auth(username, password, function(err) {
//zip files locally
output = fs.createWriteStream('./bravo.zip');
const archive = archiver('zip', {
store: true // Sets the compression method to STORE.
});
archive.on('error', function(err) {
if (err) {
console.log(err);
return;
}
});
archive.pipe(output);
if (err) {
console.log("Error", err);
return;
}
// list files from server path, then loop thru files and use
ftp.get to
// fetch them.
// inside
ftp.get callback you will use fs to write file to directory to
// test the output.
// loop thru and append them to archive, (testing) -> fs, (live) -> ftp,
// buffer temp files.
var gatherFiles = function(dir) {
return new Promise(function(resolve, reject) {
ftp.ls(dir, function(err, res) {
if (err) {
reject(err);
return;
}
// console.log(res);
var files = [];
res.forEach(function(file) {
files.push(file.name);
});
resolve(files);
});
});
}
// map to each file and stream the data through a socket as a buffer
// to store them directly in the zip archive.
gatherFiles(path).then(function(files) {
async.mapLimit(files, 1, function(file, callback) {
var bufs = [];
ftp.get(path + file, function(err, socket) {
if (err) {
console.log(err);
return;
}
socket.on("data", function(d) {
bufs.push(d);
});
socket.on("close", function(hadErr) {
if (hadErr) {
console.error('There was an error retrieving the file.');
callback(hadErr);
} else {
var buf = Buffer.concat(bufs);
archive.append(buf, {
name: file
});
callback();
}
});
socket.resume();
});
}, function(err, res) {
if (err) {
console.log(err);
return;
}
archive.finalize();
console.log('updates complete' + res);
});
});
});