你应该看看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