Search code examples
node.jsgunzip

Node GUnzip header mismatch when deflating


I'm trying to deflate a gzip archive, locally created on macOS like:

$ echo "TEST" >> test.txt
$ echo "TEST" >> test.txt
$ echo "TEST" >> test.txt
$ echo "TEST" >> test.txt
$ tar -cvzf test.gz test.txt

then in node I open a input read stream, pipe the gunzip and write back to a output stream like:

$ node
> fs=require('fs'),gunzip = zlib.createGunzip();
> fs.createReadStream( './test.gz'  ).pipe(gunzip).pipe(fs.createWriteStream('./out.txt'))

What I get is a corrupted file having a bad header (that is the tar header actually - see response) that is:

]$ cat out.txt 
out.txt000644 000765 000024 00000024000 13070721621 014300 0ustar00loretoparisistaff000000 000000 out.txt000644 000765 000024 00000000036 13070720161 014301 0ustar00loretoparisistaff000000 000000 TEST
TEST
TEST
TEST
TEST
TEST

NOTE: I have explicitly omitted the close and error events, in that case it would be:

> fs.createReadStream( './test.gz'  ).pipe(gunzip).pipe(fs.createWriteStream('./out.txt')).on('close', () => { console.log('done'); }).on('error', (error) => { console.error(error) });

but this does not affect this issue.


Solution

  • That's not a bad header, that's a tar header. Which exists because you're actually creating a gzipped tar file.

    If you just want to compress test.txt, use this:

    gzip < test.txt > test.gz