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:

{
  "name": "Save attachments to Nextcloud",
  "nodes": [
    {
      "parameters": {},
      "name": "Start",
      "type": "n8n-nodes-base.start",
      "typeVersion": 1,
      "position": [
        250,
        300
      ],
      "alwaysOutputData": true
    },
    {
      "parameters": {
        "mailbox": "n8n",
        "downloadAttachments": true,
        "options": {
          "allowUnauthorizedCerts": true
        }
      },
      "name": "IMAP Email",
      "type": "n8n-nodes-base.emailReadImap",
      "typeVersion": 1,
      "position": [
        720,
        220
      ],
      "alwaysOutputData": false,
      "credentials": {
        "imap": "[email protected]"
      }
    },
    {
      "parameters": {
        "command": "=unzip /home/node/n8n/data/{{$workflow.id}}/{{$node[\"IMAP Email\"].binary.attachment_0.fileName}} *.pdf -d /home/node/n8n/data/{{$workflow.id}}/"
      },
      "name": "Unzip attachment",
      "type": "n8n-nodes-base.executeCommand",
      "typeVersion": 1,
      "position": [
        1050,
        220
      ]
    },
    {
      "parameters": {
        "fileName": "=/home/node/n8n/data/{{$workflow.id}}/{{$node[\"IMAP Email\"].binary.attachment_0.fileName}}",
        "dataPropertyName": "attachment_0"
      },
      "name": "Save attachment",
      "type": "n8n-nodes-base.writeBinaryFile",
      "typeVersion": 1,
      "position": [
        860,
        220
      ]
    },
    {
      "parameters": {
        "filePath": "=/home/node/n8n/data/{{$workflow.id}}/{{$node[\"Set zip filename\"].json[\"zipFileName\"]}}/{{$json[\"pdfFileName\"]}}",
        "dataPropertyName": "pdf"
      },
      "name": "Read Binary File",
      "type": "n8n-nodes-base.readBinaryFile",
      "typeVersion": 1,
      "position": [
        1500,
        470
      ]
    },
    {
      "parameters": {
        "command": "=basename \"$(ls /home/node/n8n/data/{{$workflow.id}}/{{$json[\"zipFileName\"]}}/*.pdf|head -1)\""
      },
      "name": "Get PDF filename",
      "type": "n8n-nodes-base.executeCommand",
      "typeVersion": 1,
      "position": [
        1410,
        260
      ]
    },
    {
      "parameters": {
        "path": "=Documents/{{$json[\"pdfFileName\"]}}",
        "binaryDataUpload": true,
        "binaryPropertyName": "pdf"
      },
      "name": "Nextcloud",
      "type": "n8n-nodes-base.nextCloud",
      "typeVersion": 1,
      "position": [
        1700,
        470
      ],
      "credentials": {
        "nextCloudApi": "my.nexcloud.xyz"
      }
    },
    {
      "parameters": {
        "command": "=basename {{$node[\"IMAP Email\"].binary.attachment_0.fileName}} .zip"
      },
      "name": "Get zip file name without extension",
      "type": "n8n-nodes-base.executeCommand",
      "typeVersion": 1,
      "position": [
        1390,
        20
      ]
    },
    {
      "parameters": {
        "values": {
          "string": [
            {
              "name": "zipFileName",
              "value": "={{$node[\"Get zip file name without extension\"].json[\"stdout\"]}}"
            }
          ]
        },
        "options": {}
      },
      "name": "Set zip filename",
      "type": "n8n-nodes-base.set",
      "typeVersion": 1,
      "position": [
        1590,
        20
      ]
    },
    {
      "parameters": {
        "values": {
          "string": [
            {
              "name": "pdfFileName",
              "value": "={{$node[\"Get PDF filename\"].json[\"stdout\"]}}"
            }
          ]
        },
        "options": {}
      },
      "name": "Set PDF filename",
      "type": "n8n-nodes-base.set",
      "typeVersion": 1,
      "position": [
        1600,
        270
      ]
    },
    {
      "parameters": {
        "command": "=rm -rf /home/node/n8n/data/{{$workflow.id}}/*"
      },
      "name": "Execute Command",
      "type": "n8n-nodes-base.executeCommand",
      "typeVersion": 1,
      "position": [
        1900,
        470
      ]
    }
  ],
  "connections": {
    "IMAP Email": {
      "main": [
        [
          {
            "node": "Save attachment",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Save attachment": {
      "main": [
        [
          {
            "node": "Unzip attachment",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Unzip attachment": {
      "main": [
        [
          {
            "node": "Get zip file name without extension",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get PDF filename": {
      "main": [
        [
          {
            "node": "Set PDF filename",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Read Binary File": {
      "main": [
        [
          {
            "node": "Nextcloud",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get zip file name without extension": {
      "main": [
        [
          {
            "node": "Set zip filename",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Set zip filename": {
      "main": [
        [
          {
            "node": "Get PDF filename",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Set PDF filename": {
      "main": [
        [
          {
            "node": "Read Binary File",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Nextcloud": {
      "main": [
        [
          {
            "node": "Execute Command",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": true,
  "settings": {
    "timezone": "Europe/Berlin",
    "executionTimeout": 60
  },
  "id": "1"
}
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 ?