admin管理员组文章数量:1428513
Creating a node.js to auto-download a zip file, containing one text file with a list of 5000 UserID's and Usernames (CSV). An example text file would be:
12345,JSmith
728422,YPatel
Every time I run this, I get different results. I think the likely cause is I am attempting to parse the unzipped file, before unzip has finished saving to disk.
Is there a way to either asynchronously parse the unzipped data, or at least wait until unzip is plete, before trying to parse the extracted file? (I am also happy to do this whole process from memory, and avoid disk)
I am very new to node.js.
function downloadAndUnzip(URL, doWhenFinished){
require('request')
.get(URL)
.on('error', function(error) {
console.log(error)
})
.on('end', doWhenFinished)
.pipe(require('unzip').Extract({path:'./'}))
}
var parseFile = function(){
var fs = require('fs')
var array =
fs.readFileSync('usernames.txt').toString().split("\n\r");
for (i in array) {
user = array[i].split(",");
console.log("UserID: " + user[0] + " Username: " + user[1]);
}
}
}
DownloadAndUnzip(URL, parseFile())
Creating a node.js to auto-download a zip file, containing one text file with a list of 5000 UserID's and Usernames (CSV). An example text file would be:
12345,JSmith
728422,YPatel
Every time I run this, I get different results. I think the likely cause is I am attempting to parse the unzipped file, before unzip has finished saving to disk.
Is there a way to either asynchronously parse the unzipped data, or at least wait until unzip is plete, before trying to parse the extracted file? (I am also happy to do this whole process from memory, and avoid disk)
I am very new to node.js.
function downloadAndUnzip(URL, doWhenFinished){
require('request')
.get(URL)
.on('error', function(error) {
console.log(error)
})
.on('end', doWhenFinished)
.pipe(require('unzip').Extract({path:'./'}))
}
var parseFile = function(){
var fs = require('fs')
var array =
fs.readFileSync('usernames.txt').toString().split("\n\r");
for (i in array) {
user = array[i].split(",");
console.log("UserID: " + user[0] + " Username: " + user[1]);
}
}
}
DownloadAndUnzip(URL, parseFile())
Share
Improve this question
asked Mar 9, 2016 at 8:21
Mtl DevMtl Dev
1,62222 silver badges30 bronze badges
1 Answer
Reset to default 7From documentation:
Extract emits the 'close' event once the zip's contents have been fully extracted to disk.
You need to listen for 'close' event.
var unzip = require('unzip');
var unzipExtractor = unzip.Extract({ path: './'});
// listen for close event and perform parse
unzipExtractor.on('close', some_function_to_parse());
版权声明:本文标题:javascript - node.js: How to wait until "unzip" is complete before trying to parse contents - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745440646a2658433.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论