Harkirat Saluja
Harkirat Saluja

Reputation: 8114

How to find count of files in a zip through adm-zip(node.js)?

I was trying to get the number of files in my zip file. I used adm-zip.So i tried using following:-

 var unzip=require('adm-zip');

 var zip=new unzip("hello.zip");
 console.log(zip.getEntries().length);

Now in my hello.zip file I have 5 files, but the count of zip.getEntries().length is 6.

zip.getEntries() returns array of objects from what I read. Can someone please help me on this? How I get the correct count of number of files?

Upvotes: 0

Views: 2649

Answers (1)

bozzmob
bozzmob

Reputation: 12584

I am not sure why its giving 1 more than what's expected. I have 2 suggestions for you. Have a check at both.

  1. Change the path of the file that you are planning to unzip to-

    var zip=new unzip("./hello.zip");

  2. Check if there are any hidden files or links(like the .git)

Also, to answer your question on how to find the length, just validate the above points and also try this-

var zipEntries = zip.getEntries()
zipEntries.forEach(function(zipEntry) {
    count++;
}

Hope it helps! :) Happy coding!

Upvotes: 1

Related Questions