0
所以我有一個文件名數組。管道multipe文件到一個流
我需要通過每一個,閱讀它作爲一個流,逐一管道到最後一個流。
錯誤地的代碼看起來是這樣的:
var files = ['file1.txt', 'file2.txt', 'file3.txt'];
var finalStream = fs.createReadStream()//throws(i need a file path here)
(function pipeSingleFile(file){
var stream = fs.createReadStream(file);
stream.on('end', function(){
if(files.length > 0){
pipeSingleFile(files.shift());
}
});
stream.pipe(finalStream);
})(files.shift())
finalStream.pipe(someOtherStream);
finalStream.on('end', function(){
//all the contents were piped to outside
});
反正有沒有做到這一點?
首先,你finalStream應該是一個writeStream(所以'fs.createWriteStream()') – Kerstomaat
感謝,但fs.createWriteStream還需要一個文件名 –
好,然後給它一個;) – Kerstomaat