Search code examples
node.jsadm-zip

adm-zip doesn't compress data


I'm trying to use adm-zip to add files from memory to a zip file also in memory. It seems that the zip file is created correctly (the result of saving zipData can be unzipped in Windows), but the compression ratio is always zero.

This is a model of the code that I expected to work but doesn't. As can be seen from the output, "compressedData" is null and "size" and "compressedSize" are the same whatever value is passed as the file content.

var admzip = require("adm-zip")
var zip = new admzip();

zip.addFile("tmp.txt", "aaaaaaaaaaaaaaaaaaaa");
var zipData = zip.toBuffer();

console.log(zip.getEntries()[0].toString());

https://runkit.com/embed/pn5kaiir12b0

How do I get it to compress the files as well as just zipping?


Solution

  • This is an old question but to anyone who is also experiencing this issue, the reason is that the adm-zip does not compress the data until the compressedData field is accessed for the first time.

    Quote from the docs

    [Buffer] Buffer compressedData When setting compressedData, the LOC Data Header must also be present at the beginning of the Buffer. If the compressedData was set for a ZipEntry anf no other change was made to its properties (comment, extra etc), reading this property will return the same value. If changes had been made, reading this property will recompress the data and recreate the entry headers.

    If no compressedData was specified, reading this property will compress the data and create the required headers.

    The output of the compressedData Buffer contains the LOC Data Header