你應該看看sax
。 它是由isaacs開發的!
我還沒有測試過這段代碼,但是我會先寫這些代碼。
var Promise = Promise || require('es6-promise').Promise
, thr = require('through2')
, createReadStream = require('fs').createReadStream
, createUnzip = require('zlib').createUnzip
, createParser = require('sax').createStream
;
function processXml (filename) {
return new Promise(function(resolve, reject){
var unzip = createUnzip()
, xmlParser = createParser()
;
xmlParser.on('opentag', function(node){
// do stuff with the node
})
xmlParser.on('attribute', function(node){
// do more stuff with attr
})
// instead of rejecting, you may handle the error instead.
xmlParser.on('error', reject)
xmlParser.on('end', resolve)
createReadStream(filename)
.pipe(unzip)
.pipe(xmlParser)
.pipe(thr(function(chunk, enc, next){
// as soon xmlParser is done with a node, it passes down stream.
// change the chunk if you wish
next(null, newerChunk)
}))
rl = readline.createInterface({
input: unzip
, ouput: xmlParser
})
})
}
processXml('large.xml.gz').then(function(){
console.log('done')
})
.catch(function(err){
// handle error.
})
我希望幫助
你有沒有考慮寫一個原生擴展,並使用C++庫?如果你的文件很大,這可能是最好的選擇...... – ChrisCM
不知道C++ tbh。目前我可以通過解壓文件然後使用最後期限來完成,但是當我將其轉入生產環境時,權限被鎖定,因此我無法更改日誌文件夾的內容,只能從中讀取。 –
嘗試使用sudo執行您的節點進程? – ChrisCM