Srihari 4cloud
Srihari 4cloud

Reputation: 11

How can I create a zip file from array variable [fileName and FileContent]. using azure function or Azure logic app(with out any third part service)

How can I create a zip file from array variable [{"fileName":"", "FileContent" :""}]. using azure function or Azure logic app(with out any third part service)

Upvotes: 1

Views: 429

Answers (1)

Hury Shen
Hury Shen

Reputation: 15754

For now, Compress/Zip files is not supported in logic app(if we do not use third party service), you could upvote for this feature on feedback page.

But for azure function, we can implement it by code without third party service. I wrote a sample in my function, please refer to my function code below:

using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using System.IO.Compression;

namespace FunctionApp1
{
    public static class Function1
    {
        [FunctionName("Function1")]
        public static async Task<IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
            ILogger log)
        {
            log.LogInformation("C# HTTP trigger function processed a request.");

            string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
            FileItem[] data = JsonConvert.DeserializeObject<FileItem[]>(requestBody);

            //As the request data you provided is a array with multiple file items, so here use foreach to loop each file item.
            foreach (FileItem item in data)
            { 
                log.LogInformation(item.fileName);
                log.LogInformation(item.FileContent);

                using (var memoryStream = new MemoryStream())
                {
                    using (var archive = new ZipArchive(memoryStream, ZipArchiveMode.Create, true))
                    {
                        var demoFile = archive.CreateEntry(item.fileName + ".txt");

                        using (var entryStream = demoFile.Open())
                        using (var streamWriter = new StreamWriter(entryStream))
                        {
                            streamWriter.Write(item.FileContent);
                        }
                    }

                    //here I create the zip file in local, you can modify the code to create the zip file anywhere else you want.
                    using (var fileStream = new FileStream(@"D:\Temp\" + item.fileName + ".zip", FileMode.Create))
                    {
                        memoryStream.Seek(0, SeekOrigin.Begin);
                        memoryStream.CopyTo(fileStream);
                    }
                }
            }

            string responseMessage = "complete";

            return new OkObjectResult(responseMessage);
        }
    }

    public class FileItem
    {
        public string fileName { get; set; }
        public string FileContent { get; set; }
    }
}

Running the function above and request it in postman. enter image description here Then we can see the zip file was created in the path which I specified in code. enter image description here

Upvotes: 1

Related Questions