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());
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745440647a4627817.html
评论列表(0条)