# How to unzip .gz file

**URL:** <https://discourse.nodered.org/t/how-to-unzip-gz-file/29085>\
**Category:** General\
**Created:** [27 June 2020 21:14 UTC](https://discourse.nodered.org/t/how-to-unzip-gz-file/29085 "2020-06-27T21:14:33Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [27 June 2020 21:14 UTC](https://discourse.nodered.org/t/how-to-unzip-gz-file/29085/1 "2020-06-27T21:14:33Z")

</div>

Does anybody know how I can unzip a .gz file other than by using an exec node calling `gunzip`? That does work but I want it to work on Windows as well as Linux and I presume that solution won't work on Windows.

I have found [pako](http://nodeca.github.io/pako/) which does seem to have the ability to do that, but I can't work out exactly how. The gz wrapper includes the file names somehow and I don't know how to handle that. I can't find any relevant examples.

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [27 June 2020 21:34 UTC](https://discourse.nodered.org/t/how-to-unzip-gz-file/29085/2 "2020-06-27T21:34:21Z")

</div>

On windows 10 you should be able to use

```auto
gzip -d file.gz

```

But is it a .gz or a .tar.gz ?

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [28 June 2020 00:27 UTC](https://discourse.nodered.org/t/how-to-unzip-gz-file/29085/3 "2020-06-28T00:27:35Z")

</div>

There's a node for that... [https://flows.nodered.org/node/node-red-contrib-gzip](https://flows.nodered.org/node/node-red-contrib-gzip)

Not used it for ages, but I do know I've used it successfully in the past.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [28 June 2020 08:03 UTC](https://discourse.nodered.org/t/how-to-unzip-gz-file/29085/4 "2020-06-28T08:03:24Z")

</div>

> [@bakman2](#):
>
> But is it a .gz or a .tar.gz ?

It is a tar.gz but I don't want to untar it, just unzip it.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [28 June 2020 08:53 UTC](https://discourse.nodered.org/t/how-to-unzip-gz-file/29085/5 "2020-06-28T08:53:46Z")

</div>

> [@knolleary](#):
>
> [node-red-contrib-gzip (node) - Node-RED](https://flows.nodered.org/node/node-red-contrib-gzip)

I had tried that but I can't get it to work. It works ok with a text file but with a .tar.gz file unzipping to a .tar file ends up with a file shorter than it should be. The tar wrapper seems ok but the contents won't extract.  
It says in the info tab for the node "If the input is a compressed buffer it tries to decompress to a utf8 string" which I suspect is the problem, I need it to decompress to binary I think.  
It is likely a matter of specifying the right options to pako.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [28 June 2020 10:32 UTC](https://discourse.nodered.org/t/how-to-unzip-gz-file/29085/6 "2020-06-28T10:32:04Z")

</div>

Hmm, this is very odd. The output from the gzip node is exactly the right length (61952) which suggests the unzip is happening correctly. I am then feeding that into a file out node configured as below, but the resulting file is only 59559 bytes long, which suggests that the file is not all being written. I saw the comment in the help for the file node that said it would close the file after every write if I supplied the filename in the message but that didn't make any difference.  
I can't believe that there is such a basic bug in the file node, but if I send it a buffer and the file is shorter than the buffer then what on earth is going on?

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/e/ae9bf54108989c6c6553540b48fb473d502304f1.png)

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [28 June 2020 12:48 UTC](https://discourse.nodered.org/t/how-to-unzip-gz-file/29085/7 "2020-06-28T12:48:15Z")

</div>

Well I appear to have got to the bottom of this. There were two issues.

Firstly was the fact that the File out node was not correctly writing the buffer. The solution to that was to set the Encoding to Default. Why that is the case I don't know. I don't think the node should even be calling the fs.write method that takes an encoding option when it is passed a Buffer.

Secondly was that fact that the gzip node appears to expect that it should generate a string, so it doesn't work with binary files (for me at least). The fix is in node-modules/node-red-contrib-gzip/lib/gzip.js to change the unzipping code from

```auto
           var out = pako.inflate(msg.payload);
           msg.payload = new txtd.TextDecoder('utf-8').decode(out);
           node.send(msg);

```

to

```auto
           var out = pako.inflate(msg.payload);
           msg.payload = new Buffer.from(out);
           node.send(msg);

```

then all works as it should. Ideally I think the node should have a binary checkbox. However, in fact I want to do this in a created node rather than a flow so now that I know how to drive pako I believe I am good to go 🙂

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [28 June 2020 13:37 UTC](https://discourse.nodered.org/t/how-to-unzip-gz-file/29085/8 "2020-06-28T13:37:57Z")

</div>

but yes - would be good to alert the owner of the gzip node. it could at least do an isUTF test before forcing the conversion... or have an option as you suggest.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [28 June 2020 13:46 UTC](https://discourse.nodered.org/t/how-to-unzip-gz-file/29085/9 "2020-06-28T13:46:33Z")

</div>

I have submitted an Issue, but I suspect it is not much supported.

> <https://github.com/librae8226/node-red-contrib-gzip/issues/2>
>
> When used to unzip a tar.gz file (passed in a Buffer object) to a tar file the node generates a file...

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [28 June 2020 13:50 UTC](https://discourse.nodered.org/t/how-to-unzip-gz-file/29085/10 "2020-06-28T13:50:10Z")

</div>

@dceejay have you any comment on why sending a Buffer to a File Out node set to binary encoding doesn't just write the buffer out?

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [28 June 2020 15:39 UTC](https://discourse.nodered.org/t/how-to-unzip-gz-file/29085/11 "2020-06-28T15:39:40Z")

</div>

not offhand

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [12 July 2020 15:39 UTC](https://discourse.nodered.org/t/how-to-unzip-gz-file/29085/12 "2020-07-12T15:39:41Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
