簡體   English   中英

如何從數組變量 [fileName 和 FileContent] 創建 zip 文件。 使用 azure function 或 Azure 邏輯應用程序(沒有任何第三方服務)

[英]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)

如何從數組變量 [{"fileName":"", "FileContent":""}] 創建 zip 文件。 使用 azure function 或 Azure 邏輯應用程序(沒有任何第三方服務)

目前,邏輯應用程序不支持壓縮/壓縮文件(如果我們不使用第三方服務),您可以在反饋頁面上投票支持此功能。

但是對於azure function,我們可以通過代碼實現,無需第三方服務。 我在我的 function 中寫了一個示例,請參考下面我的 function 代碼:

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

運行上面的 function 並在 postman 中請求它。 在此處輸入圖像描述 然后我們可以看到 zip 文件是在我在代碼中指定的路徑中創建的。 在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM