• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

Tachytelic.net

  • Get in Touch
  • About Me

How to create a Zip file in Power Automate for free

July 26, 2021 by Paulie 37 Comments

In this post, I will explain how to create a Zip file in Power Automate without the use of any third party connectors or premium actions.

Table of contents

  • Introduction
  • Flow Detail
  • Flow Code and Implementation
  • Additional Information
  • Conclusion

Introduction

Power Automate includes functionality to extract data from Zip files. The Zip file contents can be extracted to a OneDrive or SharePoint folder:

Image showing actions in Power Automate that extract the contents of Zip files.

It’s strange that there is not a matching create archive action. An idea was posted on the community forum in 2017, but it has not been added, nor received many votes. Encodian provide this function, but many people prefer not to use external connectors.

SharePoint provides this functionality through the user interface, and I wondered if this API endpoint could be accessed from directly from Power Automate.

Image of SharePoint Online interface that allows you to create a Zip file by selecting multiple files.
By selecting multiple files in SharePoint and pressing download, a Zip file will be generated and downloaded to you.

I quickly found that it is possible to create Zip files directly from Power Automate!

The key to unlocking this functionality is in the SharePoint API action RenderListDataAsStream. I used this API action in a previous post, easy way of getting Totals from SharePoint lists. This API action can return some useful information, such as:

  • The Access Token required to access the items.
  • The size of each file.
  • The full item URL.
  • The media URL – which is used for converting and manipulating files.

By collecting this information it is possible to formulate a new request to the media conversion enpoint which can generate a Zip File.

Flow Detail

Before we get into the details, here is a screenshot of a flow which zips an entire folder:

Image of a Power Automate Flow which creates a Zip File.

Although not as convenient as a built-in action, this flow isn’t very complicated. At the end of the flow, the zip file is written to a SharePoint Document Library and a OneDrive folder.

Flow Code and Implementation

To implement this flow for yourself, you need to do the following:

  • Copy and paste the Scope Code below into one of your own flows.
  • Update the references to SharePoint and OneDrive to match your own environment.
  • Change the values in the compose action called settings
    • libraryPath is the path to the root of the SharePoint site.
    • zipFolderPath is the relative path of the folder that you want to Zip.
  • Update or delete the final storage actions appropriately for your implementation.

Scope Code:

{
  "id": "b7af8f36-c669-4d9f-bb7d-bf14-63edb83a",
  "brandColor": "#8C3900",
  "connectionReferences": {
    "shared_onedriveforbusiness": {
      "connection": {
        "id": "/providers/Microsoft.PowerApps/apis/shared_onedriveforbusiness/connections/shared-onedriveforbu-05f1da5e-297d-4a80-8df7-cb78-b654b8a3"
      }
    },
    "shared_sharepointonline": {
      "connection": {
        "id": "/providers/Microsoft.PowerApps/apis/shared_sharepointonline/connections/de645f5680b74c47bbce762c8e2d06ac"
      }
    }
  },
  "connectorDisplayName": "Control",
  "icon": "data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iMzIiIGhlaWdodD0iMzIiIHZlcnNpb249IjEuMSIgdmlld0JveD0iMCAwIDMyIDMyIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPg0KIDxwYXRoIGQ9Im0wIDBoMzJ2MzJoLTMyeiIgZmlsbD0iIzhDMzkwMCIvPg0KIDxwYXRoIGQ9Im04IDEwaDE2djEyaC0xNnptMTUgMTF2LTEwaC0xNHYxMHptLTItOHY2aC0xMHYtNnptLTEgNXYtNGgtOHY0eiIgZmlsbD0iI2ZmZiIvPg0KPC9zdmc+DQo=",
  "isTrigger": false,
  "operationName": "CreateZip",
  "operationDefinition": {
    "type": "Scope",
    "actions": {
      "accessToken": {
        "type": "Compose",
        "inputs": "@outputs('SharePointHTTP')?['body']['ListSchema']['.driveAccessToken']",
        "runAfter": {
          "SharePointHTTP": [
            "Succeeded"
          ]
        },
        "description": "Collects the Access Token required to access the files"
      },
      "Select": {
        "type": "Select",
        "inputs": {
          "from": "@body('SharePointHTTP')['ListData']['Row']",
          "select": {
            "name": "@item()['FileLeafRef']",
            "size": "@item()['SMTotalSize']",
            "docId": "@{item()['.spItemUrl']}&@{outputs('accessToken')}",
            "isFolder": "@if(equals(item()['FSObjType'],'1'), true, false)"
          }
        },
        "runAfter": {
          "accessToken": [
            "Succeeded"
          ]
        },
        "description": "Reformats the output of SharePoint HTTP action ready for submission to the Zip endpoint"
      },
      "Attachment_Items": {
        "type": "Compose",
        "inputs": {
          "items": "@body('Select')"
        },
        "runAfter": {
          "Select": [
            "Succeeded"
          ]
        },
        "description": "Additional formatting of the Select array"
      },
      "downloadZip": {
        "type": "OpenApiConnection",
        "inputs": {
          "host": {
            "connectionName": "shared_sharepointonline",
            "operationId": "HttpRequest",
            "apiId": "/providers/Microsoft.PowerApps/apis/shared_sharepointonline"
          },
          "parameters": {
            "dataset": "@body('SharePointHTTP')['ListSchema']['.mediaBaseUrl']",
            "parameters/method": "POST",
            "parameters/uri": "/transform/[email protected]{body('SharePointHTTP')['ListSchema']['.callerStack']}",
            "parameters/headers": {
              "Content-Type": "application/x-www-form-urlencoded"
            },
            "parameters/body": "zipFileName=test.zip&[email protected]{guid()}&provider=spo&[email protected]{encodeUriComponent(outputs('Attachment_Items'))}&oAuthToken="
          },
          "authentication": {
            "type": "Raw",
            "value": "@json(decodeBase64(triggerOutputs().headers['X-MS-APIM-Tokens']))['$ConnectionKey']"
          }
        },
        "runAfter": {
          "Attachment_Items": [
            "Succeeded"
          ]
        },
        "description": "Submits the request to the media server and downloads the Zip file"
      },
      "StoreZip": {
        "type": "OpenApiConnection",
        "inputs": {
          "host": {
            "connectionName": "shared_onedriveforbusiness",
            "operationId": "CreateFile",
            "apiId": "/providers/Microsoft.PowerApps/apis/shared_onedriveforbusiness"
          },
          "parameters": {
            "folderPath": "/__PA_Test/Filled Word Docs",
            "name": "testFile.zip",
            "body": "@base64ToBinary(body('downloadZip')['$content'])"
          },
          "authentication": {
            "type": "Raw",
            "value": "@json(decodeBase64(triggerOutputs().headers['X-MS-APIM-Tokens']))['$ConnectionKey']"
          }
        },
        "runAfter": {
          "downloadZip": [
            "Succeeded"
          ]
        },
        "description": "Stores the received Zip file in OneDrive",
        "runtimeConfiguration": {
          "contentTransfer": {
            "transferMode": "Chunked"
          }
        }
      },
      "settings": {
        "type": "Compose",
        "inputs": {
          "libraryPath": "/sites/PowerAutomateText/Shared Documents",
          "zipFolderPath": "/DeclarationTemplateFilled"
        },
        "runAfter": {},
        "description": "Set library path to the location of the SharePoint document library that contains the documents for compression. Set zipFolderPath to the relative path of the Folder that you want to archive."
      },
      "SharePointHTTP": {
        "type": "OpenApiConnection",
        "inputs": {
          "host": {
            "connectionName": "shared_sharepointonline",
            "operationId": "HttpRequest",
            "apiId": "/providers/Microsoft.PowerApps/apis/shared_sharepointonline"
          },
          "parameters": {
            "dataset": "https://accendo1.sharepoint.com/sites/PowerAutomateText",
            "parameters/method": "POST",
            "parameters/uri": "_api/web/GetListUsingPath([email protected])/[email protected]=%[email protected]{encodeUriComponent(outputs('settings')['libraryPath'])}%27&[email protected]{encodeUriComponent(concat(outputs('settings')['libraryPath'], outputs('settings')['zipFolderPath']))}",
            "parameters/body": "{\"parameters\": {\"RenderOptions\": 4103}}"
          },
          "authentication": {
            "type": "Raw",
            "value": "@json(decodeBase64(triggerOutputs().headers['X-MS-APIM-Tokens']))['$ConnectionKey']"
          }
        },
        "runAfter": {
          "settings": [
            "Succeeded"
          ]
        },
        "description": "Retrieves the file and folder information required to create a Zip file"
      },
      "Create_file": {
        "type": "OpenApiConnection",
        "inputs": {
          "host": {
            "connectionName": "shared_sharepointonline",
            "operationId": "CreateFile",
            "apiId": "/providers/Microsoft.PowerApps/apis/shared_sharepointonline"
          },
          "parameters": {
            "dataset": "https://accendo1.sharepoint.com/sites/PowerAutomateText",
            "folderPath": "/Shared Documents",
            "name": "testWordFile.docx",
            "body": "@base64ToBinary(body('downloadZip')['$content'])"
          },
          "authentication": {
            "type": "Raw",
            "value": "@json(decodeBase64(triggerOutputs().headers['X-MS-APIM-Tokens']))['$ConnectionKey']"
          }
        },
        "runAfter": {
          "StoreZip": [
            "Succeeded"
          ]
        },
        "description": "Stores the received Zip file in a SharePoint document library",
        "runtimeConfiguration": {
          "contentTransfer": {
            "transferMode": "Chunked"
          }
        }
      }
    },
    "runAfter": {},
    "description": "A scope to create a Zip file in Power Automate without the use of 3rd Party Connectors or Premium Actions"
  }
}

Additional Information

This flow works by sending an array of files or folder names to the media conversion endpoint as form data. A sample array looks like this:

{
  "items": [
    {
      "name": "_rels",
      "size": "1168",
      "docId": "https://accendo1.sharepoint.com:443/_api/v2.0/drives/b!wGO-FC/items/01KW24YP...?version=Published&access_token=eyJ0eXAi...",
      "isFolder": true
    },
    {
      "name": "docProps",
      "size": "2861",
      "docId": "https://accendo1.sharepoint.com:443/_api/v2.0/drives/b!wGO-FC/items/01KW24YP...?version=Published&access_token=eyJ0eXAi...",
      "isFolder": true
    },
    {
      "name": "word",
      "size": "477746",
      "docId": "https://accendo1.sharepoint.com:443/_api/v2.0/drives/b!wGO-FC/items/01KW24YP...?version=Published&access_token=eyJ0eXAi...",
      "isFolder": true
    },
    {
      "name": "[Content_Types].xml",
      "size": "2703",
      "docId": "https://accendo1.sharepoint.com:443/_api/v2.0/drives/b!wGO-FC.../items/01KW24YP...?version=Published&access_token=eyJ0eXAi...",
      "isFolder": false
    }
  ]
}

This array is created in the Select action based on the output of the SharePointHTTP action. This example flow is designed to zip an entire folder but you might have more specific requirements.

If you want to filter this array you have a choice of two places in which to do it:

  • By modifying the URI in the SharepointHTTP action to filter what it returns in the request (Examples here).
  • By adding a filter array action after the Select action and then using that filter as the input for the Compose action called Attachment Items.

Filtering the Select action is the easiest option, but less efficient.

Conclusion

I’ve been looking for a way to a create Zip file in Power Automate for a while (in order to be able to modify Word documents) so it is great to get this done.

Although this solution works well, It’s clear that a native function to do this wouldn’t be much work, so please vote the idea up if you would like to see this.

Filed Under: Power Platform Tagged With: Power Automate

Reader Interactions

Comments

  1. John From Ireland says

    July 28, 2021 at 12:03 am

    Awesome!

  2. Riste says

    September 13, 2021 at 4:14 pm

    Paul seems like it downloads whole SharePoint directory, not looking only in the subfolder

  3. Greg says

    September 14, 2021 at 3:21 am

    Seems to be limited to 30 items per the default Rowlimit = 30. Is there a way to get around this?

  4. Drew says

    September 28, 2021 at 4:13 pm

    @Greg, just commenting to answer your question.
    In the SharePointHTTP step, change the Body to:
    {
    “parameters”: {
    “RenderOptions”: 4103,
    “OverrideViewXml”: “1000”
    }
    }

  5. Barend says

    October 5, 2021 at 8:58 am

    Tx for your help Drew, unfortunately I know have an issue with the Dowload_zip action. It throws the following error: ‘The template language function ‘encodeUriComponent’ was invoked with invalid parameters. The parameter at index ‘0’ cannot be converted to URI component.’. Any suggestions?

    Tx in advance.

  6. Paulie says

    October 5, 2021 at 9:53 am

    Hi Barend,

    If you use the get in touch form, I will setup a teams session and have a look at your flow for you.

    Paulie

  7. Kiran Chauhan says

    October 14, 2021 at 9:12 am

    Hi Paul, I am getting following error on downloadZip step.

    {
    “status”: 400,
    “message”: “Unexpected response from the service\r\nclientRequestId: 1059e7e2-df8f-457f-96b7-ffd7782607a2”,
    “error”: {
    “message”: “Unexpected response from the service”
    },
    “source”: “sharepointonline-wi.azconn-wi.p.azurewebsites.net”
    }

    Thanks in Advance!

  8. Kiran Chauhan says

    October 14, 2021 at 9:30 am

    Hi Paul, Issue was with input data constructed wrongly. It is fixed now. Thanks.

  9. Paulie says

    October 14, 2021 at 10:05 am

    Well done! Glad you got it working.

  10. Daniel says

    November 3, 2021 at 7:06 pm

    Thank you for this, really helpful!

    I used an additional parameter in the Body for the SharePointHTTP to specify an exact folder of contents I want to Zip, otherwise it returns the whole Library into the Zip file:

    {“parameters”: {
    “RenderOptions”: 4103,
    “FolderServerRelativeUrl”: “/sites/yoursharepointsite/yourlibrary/yourfolder/anotherfolder”
    }
    }

  11. Paulie says

    November 3, 2021 at 7:17 pm

    Nice one Daniel! Glad you found it useful.

  12. Anuradha Pandrangi says

    November 9, 2021 at 1:58 pm

    Hi Paul,

    Thank you for this article which saved me lot of time.

    However, it is failing at download zip file when files are over 100 MB which is limit of power automation. Is there a way i can download large size files.

    What i noticed is that, when you use download functionality in SharePoint to download multiple files into zip file it is not compressing the file size. I think this is causing the issue downloading large files.

    Any suggestion are very much appreciated.

    Thanks,
    Anu

  13. Paulie says

    November 9, 2021 at 1:59 pm

    Sorry, I do not know of a way to make it work for Zip files greater than 100Mb.

  14. BeyondTheBox says

    November 13, 2021 at 3:30 pm

    Excellent code sample!

    I do have a question regarding the DownloadZip. While I see you specified a file name in the body I presume this file is not actually created but used by the transform service to properly create the zip content. Is that correct?

    I plan to alter this flow slightly in that I want to be able to point it at a folder, zip the files in the folder and then remove the files from the folder leaving just the zip. Your sample will save me a lot of time so I thank you!!

  15. BeyondTheBox says

    November 13, 2021 at 3:37 pm

    @Daniel

    I am not clear why you needed to adjust the code in the way you mentioned just above. Couldn’t you simply change the Settings action to target the specific folder and use the filter action suggested in the video skip subfolders? You presented your comment as if there is a problem flow and I just want to be sure I understand if the is or is not an actual issue.

  16. BeyondTheBox says

    November 13, 2021 at 3:42 pm

    @Paul & Anu

    I am sure the limit is based on the max size of an actions content (compose). It is possible to ask the transform service to deliver the zip file to a specific location bypassing the need to bring all of that content into the flow? In other words, point the flow at a folder, tell the HTTP Transform service to zip the files in that folder and to also create the zip file itself.

  17. Olli says

    November 17, 2021 at 12:15 pm

    Hi Paulie,

    thanks for sharing. this opens a lot of new possibilities.
    I get an error when opening the created .docx, both on SharePoint and OneDrive. Open still works but footer and header are not copied from the template. Any idea what this could be due to?

    cheers,
    olli

  18. Avik says

    November 22, 2021 at 3:29 pm

    Hi Paulie,

    Thank you for the wonderful way of creating zips.

    However, I see that the file size as well as the zip size is the same.

    Can you suggest some way to enable “compression” that zip file size is less than the original one.

    Regards,
    Avik

  19. Paulie says

    November 22, 2021 at 3:30 pm

    Hi Avik, I do not know of a way to enable compression. But please let me know if you find a way!

    Paul

  20. Beyond the Box says

    November 24, 2021 at 7:42 pm

    Quite a shock to find out that this code does not compress. You might want to make note of that in the article.

  21. Jesus Reyna says

    December 20, 2021 at 5:23 pm

    Interestingly, I’ve tried using your provided code, and am able to obtain a zip file, but the zip file is invalid and cannot be opened, any idea why this might be?

  22. vaishali says

    December 30, 2021 at 10:56 am

    I am using this. I have one main folder under that we have different subfolders and each subfolder has files. When I am converting the main folder to zip I noticed only 30 subfolders are getting created inside that/ Is this limitation or I am missing something?

  23. Doug says

    January 2, 2022 at 1:58 am

    I’m sure I’m doing something stupid but I get “401 UNAUTHORIZED” error at the downloadZip point. I am using an admin account.

  24. EM says

    January 10, 2022 at 3:47 pm

    Hi Doug, I had the same issue

    In downloadZip step, put this as site address: @body(‘SharePointHTTP’)[‘ListSchema’][‘.mediaBaseUrl’]

  25. Gabo says

    February 18, 2022 at 2:22 pm

    in downloadZip in body try zipFileName=test.zip&[email protected]{guid()}&provider=spo&files={“items”:@{encodeUriComponent(outputs(‘AttachmentItems’))}}&oAuthToken=

  26. Francis says

    February 28, 2022 at 2:29 pm

    Hi Paulie, Thanks for this!

    Unfortunately I haven’t been able to run the flow as the downloadZip action keeps failing.

    Here is the error that I encountered:
    – IIS 10.0 Detailed Error – 503.2 – Service Unavailable
    – The [email protected] setting is being exceeded.

    The action retries but it ultimately fails. Hope you can help me with this.

  27. Tim says

    March 10, 2022 at 6:19 pm

    @BeyondTheBox

    How does one request the transform service to deliver the zip file to destination?

  28. Amir Zareei says

    March 18, 2022 at 10:21 pm

    Hi Paulie,
    Hope you are doing well.

    I’m having the same issue that Greg was experiencing. I tried using
    n the SharePointHTTP step, change the Body to:
    {
    “parameters”: {
    “RenderOptions”: 4103,
    “OverrideViewXml”: “1000”
    }
    }
    but it still doesn’t return more than 30 items

    Any idea what could be the problem?

    Thanks a lot
    Amir

  29. Angela Bean says

    March 25, 2022 at 9:11 pm

    I have a OneDrive business account but do not have Sharepoint. Is there still a way to make this work?

    Thanks

  30. Paulie says

    March 25, 2022 at 9:13 pm

    I bet there is a way. I haven’t looked myself but I can look into it for you.

  31. Shiva says

    April 22, 2022 at 11:33 am

    Hi Paulie ,
    InvalidTemplate. Unable to process template language expressions in action ‘downloadZip’ inputs at line ‘0’ and column ‘0’: ‘The template language function ‘uriComponent’ was invoked with invalid parameters. The parameter at index ‘0’ cannot be converted to URI component.’.
    This was error I am getting could you please help me

  32. Tanoh says

    June 23, 2022 at 9:58 am

    Hi Paulie,

    Good Day!

    I hope you’re having a great week!

    Please advise what if the files are more than a 30.

    It appears that the recommendation of Drew doesn’t work.

    Thanks,
    Tanoh

  33. Charl says

    July 12, 2022 at 9:32 am

    Hi,

    I’m hoping somebody still checks these comments and can help me. I get a binary file on DownloadZip, but it’s invalid. Check it in a HEX editor shows the following:

    PK………A.T……….
    ..,…__IApplicationDocu
    mentGenerator.cs_Error.t
    xtThis file cannot be do
    wnloaded. ….ExceptionT
    ype: BadArgumentMeTAExce
    ption. ….CorrelationId
    : f70ce463-05a7-4791-8d5
    d-63e753a276bf, ….UTC
    DateTime: 7/12/2022 8:13
    :57 AMPK…..”……..

    Note that with different contect, I error above is always for the first file encountered.

    I can’t find any information on badArgumentMeTAException, so I don’t know what I’m doing wrong. I’ve verified my version of each corresponding step in the flow against the example code here, and can’t spot any difference.

    Any thoughts on what I can try or where to look for answers?

    Kind regards,
    Charl Marais

  34. Charl says

    July 12, 2022 at 9:47 am

    Hi,

    Found my mistake. I went over all the flow steps again to compare and found that I missed the ‘&’ before the access token on the Select step.

    Thanks for this flow Paulie!

    Kind regards,
    Charl Marais

  35. Bene says

    July 15, 2022 at 9:43 am

    Hi,

    i got the following Problem:

    Template language expressions in the inputs of the “SharePointHTTP” action in row “0” and column “0” cannot be processed: The template language expression “uriHost(encodeUriComponent(concat(outputs(‘settings’)[‘libraryPath’], outputs(‘settings’)[‘libraryPath’]))” cannot be evaluated because the “libraryPath” property cannot be selected. Array elements can only be selected with an integer index. For more information on usage, see

    I connected every Step as you Mention, can you help me

    Kind Regards

    Ben

  36. Pat says

    July 29, 2022 at 10:28 am

    Hi,

    So, this can’t be achieved if the source files are in Onedrive?

    Regards,
    Pat

  37. Manuela Carreño says

    August 1, 2022 at 10:45 pm

    Hi!! I tried out this solution but it creates a Zip file with all the documents of the shared documents folder, what could I do?

Leave a Reply Cancel reply

Primary Sidebar

Link to my LinkedIn Profile
Buy me a coffee
Image link to all Power Automate content

Excellent Power Automate Blogs

  • Damien Bird
  • Dennis (Expiscornovus)
  • Tom Riha

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 270 other subscribers.

Go to mobile version