javascript - node.js: How to wait until "unzip" is complete before trying to parse contents - Stack Overflow

Creating a node.js to auto-download a zip file, containing one text file with a list of 5000 UserID

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
Add a ment  | 

1 Answer 1

Reset to default 7

From 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条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信