Since Microsoft Dynamics 365 for Finance & Operations is a cloud-based ERP we cannot work with files on the AOS drive anymore. It was pretty usual to have file-based integrations in AX where you got a file in a folder and processed it.

Of course it’s still possible to work with files, for example from a storage account on Azure like Miquel Vidal has shown in his blog (in Spanish) or with the Recurring Integrations Scheduler tool.

Unzip .NET Streams

Most of the functionalities that consist in uploading of downloading a file is MSDyn365FO use .NET Streams, usually its child class MemoryStream.

So, how do we unzip one of these files? For example, the ISO20022 vendor payment journal file is zipped. What if we need to access the XML file contained in the ZIP?

We’ll need to use the ZipArchive class from System.IO.Compression namespace, and it’s really really easy. For example:

public MemoryStream unizpStream(MemoryStream _stream)
{
    ZipArchive        zipArchive   = new ZipArchive(_stream);
    ZipArchiveEntry   entry;    
    MemoryStream      retStream    = new MemoryStream();

    var enumerator = zipArchive.Entries.GetEnumerator();

    while (enumerator.MoveNext())
    {
        entry = enumerator.Current;.
        break;
    }

    var unzippedStream = entry.Open();

    unzippedStream.CopyTo(retStream);

    return retStream;
}

Edit: this code is valid for a single file ZIP. If the zipped file contains more than one file you must process each Stream inside the while.

We need to copy the unzippedStream stream (which is a DeflateStream) to a MemoryStream (which must be initialized) before returning it.

Remember that to access a .NET collection we need to use an enumerator to loop through it. If you don’t see the methods in the lookup using dot notation just write them, writing .NET code in X++ is still a bit buggy…

Subscribe!

Receive an email when a new post is published
Author

Microsoft Dynamics 365 Finance & Operations technical architect and developer. Business Applications MVP since 2020.

Write A Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Exit mobile version