X++ Code to Create Zip Files in D365 - D365 F&O

Hello Everyone,

Today we will be looking at, how to create a Zip file by using X++ code, while exporting data from the system. Let us see how the code is build.

using System.IO.Compression;

public void createZipFile(Map _cStoreErrorData)
{
    MapEnumerator mapEnumerator = _cStoreErrorData.getEnumerator();
       
        if (_cStoreErrorData.elements() > 0)
        {
            using(System.IO.MemoryStream zipStream  = new System.IO.MemoryStream())
            {
                using(System.IO.Compression.ZipArchive archive  = new System.IO.Compression.ZipArchive(zipStream, System.IO.Compression.ZipArchiveMode::Create, true))
                {
                    while(mapEnumerator.moveNext())
                    {
                        ZipArchiveEntry dataFileEntry   = archive.CreateEntry(mapEnumerator.currentKey());

                        using (System.IO.Stream dataFileEntryStream = dataFileEntry.Open())
                        {
                            System.IO.Stream stream = mapEnumerator.currentValue();
                            stream.CopyTo(dataFileEntryStream);
                        }
                    }
                }
                File::SendFileToUser(zipStream, <Your File Name>; + '.zip');
            }
        }
}

Proud To Be a DAX Developer :-)

No comments:

Post a Comment