azure - 如何从数组变量 [fileName 和 FileContent] 创建 zip 文件。使用azure函数或Azure逻辑应用程序(没有任何第三方服务)

标签 azure zip azure-logic-apps azure-function-app

如何从数组变量 [{"fileName":"", "FileContent":""}] 创建 zip 文件。使用azure函数或Azure逻辑应用程序(没有任何第三方服务)

最佳答案

目前,逻辑应用不支持压缩/Zip 文件(如果我们不使用第三方服务),您可以在 feedback 上为此功能投票。页。

但是对于azure功能,我们可以通过代码来实现,无需第三方服务。我在函数中写了一个示例,请引用下面的函数代码:

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; }
    }
}

运行上面的函数并在 postman 中请求它。 enter image description here 然后我们可以看到 zip 文件是在我在代码中指定的路径中创建的。 enter image description here

关于azure - 如何从数组变量 [fileName 和 FileContent] 创建 zip 文件。使用azure函数或Azure逻辑应用程序(没有任何第三方服务),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65200572/

相关文章:

azure - 逻辑应用中的嵌套 foreach 循环不应用并发控制设置

asp.net-mvc - 使用 Windows Azure Active Directory (WAAD) 转变声明

azure - "message": "Call to Microsoft.Web/sites failed. Error message: SkuCode ' SKU ' is invalid.",

azure - Azure Functions 的筛选器

java - 如何判断加密头是否存在?

azure - 逻辑应用连接器策略引发 "Property Immutable"错误

Azure staticwebapp 不允许在终端中使用 Azure CLI 添加值为 "&"的配置

java - 将 XML 文件与其他 PDF 文件一起附加在 zip 文件中

visual-c++ - 使用 boost 和 Visual C++ 2005 解压缩 zip 文件?

azure - 有没有办法通过复制粘贴将分支添加到逻辑应用程序中?