Unarchive zip files

I’m creating a workflow which includes email attachments. Those attachments arrive in the form of zip files. I need to unpack them and then work with the contents.

However, I couldn’t find an unarchive/unpack workflow node. Did I overlook something? Is it supposed to be done with the functions node and some javascript?

1 Like

@ubuntudroid welcome to the community.

There not node to unpack a zip file yet. Using a function node and perhaps a library like this it could be posible.

1 Like

Or execute the command directly on the machine itself with the “Execute Command” node

1 Like

Many thanks everyone! I will try out both approaches and report back here with which one I’ve ended up.

After quite a bit of fiddling I went with Damian_Ks approach and unzipped on the machine itself.

I’m pretty sure it is unnecessarily complicated but it works quite well now. If anyone spots potential to improve the flow I’d be happy to hear about it! :slight_smile:

2 Likes

@ubuntudroid Created a node to compress/decompress zip and gzip files. Can you please test it locally and provide feedback?

1 Like

@RicardoE105 Unfortunately I’m super busy right now and can’t test this out just yet. I’ve scheduled that for the next weekend. In the meantime, could you tell me how to install the new node on my self hosted instance? I’m running the docker raspberry pi container.

Hi @RicardoE105, just tested. Seems to be working fine for .zip files.
Tried a couple .rar and am needing .gz but these seemed not to work. (dont know if they shall with your code).

Below the example, and attached the file I used sent through postman.
https://drive.google.com/file/d/11RmsjHNHop39nKS9iSPY7DdEZS63hpyT/view?usp=sharing

Should the .gz files work? (u have a name.xls.gz that need to use)
On the other hand, on the file I sent you can see that it doesnt have headers and I am having trouble getting some headers prior to the first line. Any idea on how this might be done?
Also when compressing again, It maked a file for each line, any idea on how to merge to the same 1 file and 1 zip file?
Thanks! and great job with the compress node!

{
  "nodes": [
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "testfile",
        "options": {
          "binaryData": true,
          "binaryPropertyName": "data"
        }
      },
      "name": "Webhook",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 1,
      "position": [
        430,
        300
      ],
      "webhookId": "ef7fed05-f51b-49bc-b911-e300302fb4af"
    },
    {
      "parameters": {
        "binaryPropertyName": "data0"
      },
      "name": "Compression1",
      "type": "n8n-nodes-base.compression",
      "typeVersion": 1,
      "position": [
        640,
        300
      ]
    },
    {
      "parameters": {
        "binaryPropertyName": "file_0",
        "options": {
          "rawData": true
        }
      },
      "name": "Spreadsheet File",
      "type": "n8n-nodes-base.spreadsheetFile",
      "typeVersion": 1,
      "position": [
        840,
        300
      ]
    },
    {
      "parameters": {
        "mode": "jsonToBinary",
        "destinationKey": "modcli",
        "options": {}
      },
      "name": "Move Binary Data",
      "type": "n8n-nodes-base.moveBinaryData",
      "typeVersion": 1,
      "position": [
        1040,
        300
      ]
    },
    {
      "parameters": {
        "operation": "compress",
        "binaryPropertyName": "modcli",
        "outputFormat": "zip",
        "fileName": "clientes.zip",
        "binaryPropertyOutput": "clientes"
      },
      "name": "Compression",
      "type": "n8n-nodes-base.compression",
      "typeVersion": 1,
      "position": [
        1240,
        300
      ]
    }
  ],
  "connections": {
    "Webhook": {
      "main": [
        [
          {
            "node": "Compression1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Compression1": {
      "main": [
        [
          {
            "node": "Spreadsheet File",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Spreadsheet File": {
      "main": [
        [
          {
            "node": "Move Binary Data",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Move Binary Data": {
      "main": [
        [
          {
            "node": "Compression",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

Hey, @pradilla, thanks for testing.

Currently, it works just with .zip and .gz.

On the other hand, on the file I sent you can see that it doesnt have headers and I am having trouble getting some headers prior to the first line. Any idea on how this might be done?

Also when compressing again, It maked a file for each line, any idea on how to merge to the same 1 file and 1 zip file?

Check the example below. Use this zip to send the request to the webhook. I added the headers with the function node as there is no “easy” way to do this currently.

Example
 {
      "nodes": [
        {
          "parameters": {
            "httpMethod": "POST",
            "path": "testfile",
            "options": {
              "binaryData": true,
              "binaryPropertyName": "data"
            }
          },
          "name": "Webhook",
          "type": "n8n-nodes-base.webhook",
          "typeVersion": 1,
          "position": [
            440,
            300
          ],
          "webhookId": "ef7fed05-f51b-49bc-b911-e300302fb4af"
        },
        {
          "parameters": {
            "binaryPropertyName": "data0"
          },
          "name": "Compression1",
          "type": "n8n-nodes-base.compression",
          "typeVersion": 1,
          "position": [
            680,
            300
          ]
        },
        {
          "parameters": {
            "binaryPropertyName": "file_0",
            "options": {
              "rawData": true
            }
          },
          "name": "Spreadsheet File",
          "type": "n8n-nodes-base.spreadsheetFile",
          "typeVersion": 1,
          "position": [
            1190,
            300
          ]
        },
        {
          "parameters": {
            "mode": "jsonToBinary",
            "destinationKey": "modcli",
            "options": {}
          },
          "name": "Move Binary Data",
          "type": "n8n-nodes-base.moveBinaryData",
          "typeVersion": 1,
          "position": [
            1460,
            300
          ]
        },
        {
          "parameters": {
            "operation": "compress",
            "binaryPropertyName": "modcli",
            "outputFormat": "zip",
            "fileName": "clientes.zip",
            "binaryPropertyOutput": "clientes"
          },
          "name": "Compression",
          "type": "n8n-nodes-base.compression",
          "typeVersion": 1,
          "position": [
            1730,
            300
          ]
        },
        {
          "parameters": {
            "functionCode": "let data = Buffer.from(items[0].binary[\"file_0\"].data, 'base64').toString()\n\n// we add the headers\ndata= 'name,age\\n' + data;\n\nconsole.log(data);\n\nreturn [\n  {\n    json: {},\n    binary: {\n      file_0: {\n            fileName: items[0].binary[\"file_0\"].fileName,\n            data: Buffer.from(data).toString('base64'),\n            mimeType: items[0].binary[\"file_0\"].mimeType,\n            fileExtension: items[0].binary[\"file_0\"].fileExtension,\n      }\n    }\n  },\n]\n"
          },
          "name": "Function",
          "type": "n8n-nodes-base.function",
          "typeVersion": 1,
          "position": [
            930,
            300
          ]
        }
      ],
      "connections": {
        "Webhook": {
          "main": [
            [
              {
                "node": "Compression1",
                "type": "main",
                "index": 0
              }
            ]
          ]
        },
        "Compression1": {
          "main": [
            [
              {
                "node": "Function",
                "type": "main",
                "index": 0
              }
            ]
          ]
        },
        "Spreadsheet File": {
          "main": [
            [
              {
                "node": "Move Binary Data",
                "type": "main",
                "index": 0
              }
            ]
          ]
        },
        "Move Binary Data": {
          "main": [
            [
              {
                "node": "Compression",
                "type": "main",
                "index": 0
              }
            ]
          ]
        },
        "Function": {
          "main": [
            [
              {
                "node": "Spreadsheet File",
                "type": "main",
                "index": 0
              }
            ]
          ]
        }
      }
    }

It maked a file for each line, any idea on how to merge to the same 1 file and 1 zip file?

I do not understand this. Can you elaborate?

It maked a file for each line, any idea on how to merge to the same 1 file and 1 zip file?

Sure! @RicardoE105,
If you add lets say 5 more lines to your file, you get at the end 5 binary files 1 per each line. And what would make sense is to have the same 1 zip file with the csv containing 5 lines.
Let me know id this is clear now.
Thanks!

I think the issue it’s that you are using the Move Binary Data node. It maps the binary data (the csv) to JSON. You do not want that. You probably have to delete that node, change the spreadsheet node to write to file and connect it to the compression node. Hope that makes sense.

Does it works with tar files ?