我尝试将文件从远程FTP服务器下载到我的本地文件夹,并使用ftp
模块将其解压缩:
var unzipper = require('unzipper');
var Client = require('ftp');
var c = new Client(); //init ftp client
var connectionProperties = {
host: "ftp.someurl.com",
user: "user",
password: "past",
};
c.connect(connectionProperties);
c.list('ftp/folder',function (err, list) {
list.map((element,i)=>{
c.get(element.name, function (err, stream) { //get file from ftp
if (err) throw err;
stream.pipe(unzipper.Extract({path:'local/folder/'+element.name})).promise().then((i)=>{
console.log('File '+element.name+' unziped');
})
stream.once('close', function() {
c.end();
});
})
})
if(i==element.length-1) {
console.log('Done!');
c.end();
}
})
但是对于list
中的许多文件,我有连接错误:
Error: Unable to make data connection
at Socket.<anonymous> (/Users/user/node_test/node_modules/ftp/lib/connection.js:935:10)
at Object.onceWrapper (events.js:285:13)
at Socket.emit (events.js:202:15)
at Object.cb (/Users/user/node_test/node_modules/ftp/lib/connection.js:575:18)
at Parser.<anonymous> (/Users/user/node_test/node_modules/ftp/lib/connection.js:117:20)
at Parser.emit (events.js:197:13)
at Parser._write (/Users/user/node_test/node_modules/ftp/lib/parser.js:59:10)
at doWrite (_stream_writable.js:415:12)
at writeOrBuffer (_stream_writable.js:399:5)
at Parser.Writable.write (_stream_writable.js:299:11)
at Socket.ondata (/Users/user/node_test/node_modules/ftp/lib/connection.js:273:20)
at Socket.emit (events.js:197:13)
at addChunk (_stream_readable.js:288:12)
at readableAddChunk (_stream_readable.js:269:11)
at Socket.Readable.push (_stream_readable.js:224:10)
at TCP.onStreamRead [as onread] (internal/stream_base_commons.js:145:17)
有什么想法吗?也许有人知道更有用的方法?
更新
现在我尝试对文件使用async.mapLimit
:
var fs = require('fs');
var unzipper = require('unzipper');
var async = require('async');
var Client = require('ftp');
//FTP Init
var c = new Client(); //init ftp client
var connectionProperties = {
host: "ftp.server.com",
user: "user",
password: "pass",
};
c.connect(connectionProperties);
var folders=['folder1','folder2','folder3','folder4','folder5'];
//Get folder file list
var listFile = function(ftp_files_folder){
return new Promise(function(resolve, reject){
c.list(ftp_files_folder,function (err, list) {
if (err) reject(err)
resolve(list)
})
})
}
//Get and unzipfiles from all folders
async.mapLimit(folders, 1, function(folder, callback){
var local_folder=__dirname+folder;
var ftp_files_folder=folder;
listFile(ftp_files_folder).then((paths)=>{
console.log('Checking files for: '+folder);
async.mapLimit(paths, 1, function(file, callback){
var file_type='.xml';
var ftp_file_path=ftp_files_folder+'/'+file; //get ftp path to file
var local_file_path=local_folder+'/'+file; //get local path to file
var local_subfolder=local_file_path.split(file_type).shift(); //get local subfolder name
if(!fs.existsSync(local_subfolder)){
c.get(ftp_file_path, function (err, stream) {
if(err){
console.log('Error getting ' + element)
callback(err)
}else{
stream.pipe(unzipper.Extract({path: local_subfolder})).on('finish', ()=>{
console.log('==File '+file+' unziped==');
callback();
})
}
})
} else {
callback();
}
}, function(err, res){
if(err) console.log(err)
console.log('Updates for '+folder+' complete');
callback();
})
})
}, function(err, res){
if(err) console.log(err);
if(res.length==folders.length) c.end();
console.log('Done!');
})