Matteo Monti
Matteo Monti

Reputation: 8940

How to compress and decompress a .tar.gz with zlib and nodejs

I am pretty sure this is quite a dumb question. However, I am in need to take a folder and compress it to a .tar.gz file using (if possible) only the zlib library of Node. Symmetrically, I would then need to decompress the .tar.gz file to a given destination folder.

I looked around but I could not find any reasonable solution to the problem that doesn't make use of external libraries, which I would like to avoid. If this cannot be done with .tar.gz, but can be done with any other compression method using just zlib, it would be great if you could tell me.

Any snippet or example would be greatly appreciated!

Upvotes: 2

Views: 4277

Answers (1)

Mark Adler
Mark Adler

Reputation: 112374

zlib will handle the ".gz" part for you, but not the ".tar" part. The tar format is not terribly complicated, so it should be straightforward to roll your own. It's just a 512 byte header before each file, the file padded out to a multiple of 512 bytes, repeated for each file, and ended with two 512-byte blocks of all zeros.

You would then use zlib to compress that using the gzip method.

Upvotes: 4

Related Questions