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
Power Automate includes functionality to extract data from Zip files. The Zip file contents can be extracted to a OneDrive or SharePoint folder:
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.
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:
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/zip?cs=@{body('SharePointHTTP')['ListSchema']['.callerStack']}", "parameters/headers": { "Content-Type": "application/x-www-form-urlencoded" }, "parameters/body": "zipFileName=test.zip&guid=@{guid()}&provider=spo&files=@{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(DecodedUrl=@a1)/RenderListDataAsStream?@a1=%27@{encodeUriComponent(outputs('settings')['libraryPath'])}%27&RootFolder=@{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.
John From Ireland says
Awesome!
Riste says
Paul seems like it downloads whole SharePoint directory, not looking only in the subfolder
Greg says
Seems to be limited to 30 items per the default Rowlimit = 30. Is there a way to get around this?
Drew says
@Greg, just commenting to answer your question.
In the SharePointHTTP step, change the Body to:
{
“parameters”: {
“RenderOptions”: 4103,
“OverrideViewXml”: “1000”
}
}
Barend says
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.
Paulie says
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
Kiran Chauhan says
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!
Kiran Chauhan says
Hi Paul, Issue was with input data constructed wrongly. It is fixed now. Thanks.
Paulie says
Well done! Glad you got it working.
Daniel says
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”
}
}
Paulie says
Nice one Daniel! Glad you found it useful.
Anuradha Pandrangi says
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
Paulie says
Sorry, I do not know of a way to make it work for Zip files greater than 100Mb.
BeyondTheBox says
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!!
BeyondTheBox says
@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.
BeyondTheBox says
@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.
Olli says
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
Avik says
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
Paulie says
Hi Avik, I do not know of a way to enable compression. But please let me know if you find a way!
Paul
Beyond the Box says
Quite a shock to find out that this code does not compress. You might want to make note of that in the article.
Jesus Reyna says
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?
vaishali says
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?
Doug says
I’m sure I’m doing something stupid but I get “401 UNAUTHORIZED” error at the downloadZip point. I am using an admin account.
EM says
Hi Doug, I had the same issue
In downloadZip step, put this as site address: @body(‘SharePointHTTP’)[‘ListSchema’][‘.mediaBaseUrl’]
Gabo says
in downloadZip in body try zipFileName=test.zip&guid=@{guid()}&provider=spo&files={“items”:@{encodeUriComponent(outputs(‘AttachmentItems’))}}&oAuthToken=
Francis says
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 serverRuntime@appConcurrentRequestLimit setting is being exceeded.
The action retries but it ultimately fails. Hope you can help me with this.
Tim says
@BeyondTheBox
How does one request the transform service to deliver the zip file to destination?
Amir Zareei says
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
Angela Bean says
I have a OneDrive business account but do not have Sharepoint. Is there still a way to make this work?
Thanks
Paulie says
I bet there is a way. I haven’t looked myself but I can look into it for you.
Shiva says
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
Tanoh says
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