node.js - Return number of files at ZIP package with NodeJS -
here function number of files within zip package.
// check if .zip package contains @ least 1 html file , return number of files function validatearchive(path, callback) { var filescount = 0; var unzipparser = unzip.parse(); var readstream = fs.createreadstream(path).pipe(unzipparser); unzipparser.on('error', function(err) { throw err; }); readstream.on('entry', function (entry) { var filename = entry.path; var type = entry.type; // 'directory' or 'file' if (type == 'file') { var fext = filename.split('.')[1]; if (fext === 'html') { filescount++; } } entry.autodrain(); }); // returns number of files settimeout(function () { callback(filescount); }, 1000); }
as can see have problem returning number of files because asynchronous process in place.
any ideas hot return number of files without using settimeout
method?
you can listen close
event:
unzipparser.on('close', function() { callback(filescount); });
which emitted when end of zip reached.
Comments
Post a Comment