Unzipping DMF Data Package Files Using Azure Function App C#

 

In this blog, we will walk through the process of unzipping a file received from D365 FO DMF (Data Management Framework) data packages using an Azure Function App.

Below are the detailed steps to implement this solution.


🔹 Step 1: Access Azure Portal

  1. Navigate to the Microsoft Azure Portal

  2. Sign in with your credentials

  3. Ensure you have the required permissions to create resources (Function App, Storage Account, etc.)


🔹 Step 2: Create a Function App

  1. In the Azure Portal, click on “Create a resource”

  2. Search for Function App and click Create

  3. Provide the required details:

    • Subscription: Select your subscription

    • Resource Group: Create or use existing

    • Function App Name: e.g., UnZipFileProcesser

    • Runtime Stack: .NET / Node.js (depending on your preference)

    • Region: Choose closest region

  4. Under Hosting:

    • Select or create a Storage Account

  5. Review + Create → Click Create

✅ Once deployed, your Function App will be ready to host your code.


🔹 Step 3: Create Azure Function Project in Visual Studio

  1. Open Microsoft Visual Studio

  2. Click on Create a new project

  3. Select Azure Functions

  4. Click Next

Configure Project:

  • Project Name: UnZipFileProcesser

  • Location: Choose your workspace

  • Click Create

Now your project will look like this and install all the packages that are showing in the project.





🔹 Step 4: Implement Unzip Logic

Inside your function, write logic to:

  1. Read the zipped file from Blob Storage

  2. Extract contents

  3. Upload extracted files to another container (e.g., output-container)

Sample C# Code Snippet:


using Azure.Storage.Blobs;
using Azure.Storage.Blobs.Specialized;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Azure.WebJobs;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using System;
using System.IO;
using System.IO.Compression;
using System.Net;

namespace UnZipFileProcesser
{
    public class UnzipFile
    {

        [Function("UnzipFile")]
        public static async Task<IActionResult> Run(
        [HttpTrigger(AuthorizationLevel.Function, "post")] HttpRequest req, ILogger _logger)
        {
            try
            {
                // Inputs from Logic App / request
                string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
                dynamic data = JsonConvert.DeserializeObject(requestBody);

                string sourceContainer = data?.sourceContainer;
                string destinationContainer = data?.destinationContainer;
                string sourcePath = data?.sourcePath;// Pass file name + Path
                string dataFileName = data?.dataFileName;
                string destinationPath = data?.destinationPath;
                string filename = data?.filename;
                string connectionString = data?.connectionString;

                var blobServiceClient = new BlobServiceClient(connectionString);

                var sourceContainerClient = blobServiceClient.GetBlobContainerClient(sourceContainer);
                var destContainerClient = blobServiceClient.GetBlobContainerClient(destinationContainer);

                await destContainerClient.CreateIfNotExistsAsync();

                var sourceBlob = sourceContainerClient.GetBlobClient(sourcePath);

                var extractedFiles = new List<string>();

                var memoryStream = new MemoryStream();
                await sourceBlob.DownloadToAsync(memoryStream);
                memoryStream.Position = 0; // Reset position before reading

                using (var archive = new ZipArchive(memoryStream, ZipArchiveMode.Read))
                {
                    foreach (var entry in archive.Entries)
                    {
                        if (string.IsNullOrEmpty(entry.Name)) continue;
                        if (!string.Equals(entry.Name, dataFileName, StringComparison.OrdinalIgnoreCase))
                            continue;

                        string blobpath = $"{destinationPath.TrimEnd('/')}/" + filename + ".csv";

                        var destBlob = destContainerClient.GetBlobClient(blobpath);

                        using var entryStream = entry.Open();
                        await destBlob.UploadAsync(entryStream, overwrite: true);

                        extractedFiles.Add(blobpath);
                    }
                }

                return new OkObjectResult(new
                {
                    Message = "Extraction complete",
                    Files = extractedFiles
                });
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "error occurred!");
                return new StatusCodeResult((int)HttpStatusCode.InternalServerError);
            }
        }
    }
}
For the above function app pass the below parameters as request body
   
{
  "connectionString": "Blob connection string",
"sourceContainer": "SourceFilePath container (Outbound Con)",
"destinationContainer": "Dest Container",
"sourcePath": "Filepath",
"destinationPath": "Destination file path (Output)",
"filename": "File name",
"dataFileName": "purchase order lines entity.txt"
}

🔹 Step 5: Publish the Function App

  1. In Visual Studio:

    • Right-click project → Publish

  2. Choose:

    • Azure

    • Azure Function App (Windows/Linux)

  3. Select your created Function App (UnZipFileProcesser)

  4. Click Publish

✅ Your code will be deployed to Azure.


🔹 Step 6: Test the Solution

  1. Upload a .zip file (DMF package) into the input container

  2. The Blob-triggered function will:

    • Automatically execute

    • Extract contents

    • Save files to output container





🚀 Summary

By following the above steps, you can:

  • Automatically process DMF package files

  • Extract zipped contents

  • Integrate with downstream systems (like Logic Apps or D365 FO)




Thanks, keep Daxing !!

No comments:

Post a Comment