thejoshwolfe / yazl

yet another zip library for node
MIT License
341 stars 45 forks source link

Option for addBuffer to compress if smaller? #54

Open AlexanderOMara opened 5 years ago

AlexanderOMara commented 5 years ago

It would be nice to have an option to have addBuffer compress the buffer if that makes the resulting file smaller.

I'm not sure it would really work for addFile but it would for addBuffer because the entire buffer is available at once.

I can make the pull request if the API can be decided on. Maybe one of the following?

addBuffer(buffer, path, {
    compress: null
});
addBuffer(buffer, path, {
    compress: 'auto'
});
addBuffer(buffer, path, {
    compress: 'smaller'
});
thejoshwolfe commented 1 month ago

Hi @AlexanderOMara sorry for the delayed response.

What you're suggesting is very reasonable, and easy to implement for addBuffer(). However, I'm concerned that this is such a useful feature that it would probably be desirable in general, which puts an awkward incentive for users of this API to use addBuffer() for everything, arguably the least performant way to add files.

I'm interested in coming up with a way to automatically enable/disable compression for all of the add*() APIs rather than just for addBuffer(). Perhaps a small buffer of initial bytes could be previewed and tested through compression to see if it yields a promising ratio, and then the full stream could be processed after that? That would introduce significant complexity to the stream management, which I'm already a little uncertain about with the node stdlib.