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.