Lukas Lukas - 5 months ago 19
Node.js Question

Get md5 checksums of entries in zip using adm-zip

I am trying to get MD5 checksums for all files in a ZIP file. I am currently using

adm-zip
for this because I read I can read zip contents into the memory without having to extract a file to the disk. But I am failing to read the data of entries in a ZIP file. My code goes as follows:

var zip = new AdmZip(path);
zip.getEntries()
.map(entry => { console.log(entry.entryName, entry.data); });


The
entryName
can be read, so opening and reading the zip works. But
data
is always
undefined
. I read that
data
is not really the method to read the data of an entry, but I am not sure how to actually read it.

Answer

To read the data of the entry, you must call the method getData() of the entry object, which returns a Buffer. Here is the updated code snippet which works on my end :

var zip = new AdmZip(path);
zip.getEntries().map(entry => {
  const md5Hash = crypto.createHash('md5').update(entry.getData()).digest('hex');
  console.log(md5Hash);
});

I used the basic crypto module to produce the md5 hash (in hex format). Don't forget to add it to the list of your requires at the top of your file: const crypto = require('crypto');