Hello Team!

Today I have not very often but very interesting scenario:

  1. I have *.zip archive in the cloud with *.xml file inside;
  2. I want to download this archive, extract file and read one element.
  3. I will use AL language and cloud version of Business Central.

This is my archive. I put it into Azure BLOB container and feel free to use it (but I'm not guarantee that it will be available all the time).

This is my repository with source code. It's absolutely #ReadyToGo. You can clone it, publish to your demo tenant and immediately test. If you use on-premise version - do not forget to change launch.json file.

Let me explain some details:

  • To download Zip file I used next function:
    procedure ReadZip();
    var
        Client: HttpClient;
        ResponseMessage: HttpResponseMessage;
        ResponseText: Text;
        ResponseStream: InStream;
    begin
        If not Client.Get(URL, ResponseMessage) then
            Error(Text001_Err);

        if not ResponseMessage.IsSuccessStatusCode() then begin
            ResponseMessage.Content().ReadAs(ResponseText);
            Error(Text002_Err,
                  ResponseMessage.HttpStatusCode(),
                  ResponseText);
        end;

        ResponseMessage.Content().ReadAs(ResponseStream);
        UnzipResponse(ResponseStream);
    end;

  • Business Central has built-in codeunit to work with ZIP files - "Zip Stream Wrapper". I've created my function "UnzipResponse" to read it:
local procedure UnzipResponse(ResponseInStream: InStream);
    var
        TempNameValueBuffer: Record "Name/Value Buffer" temporary;
        TempBlob: Record TempBlob temporary;
        ZipMgt: Codeunit "Zip Stream Wrapper";
        ResponseOutStream: OutStream;
    begin
        ZipMgt.OpenZipFromStream(ResponseInStream, false);
        TempNameValueBuffer.Reset();
        TempNameValueBuffer.DeleteAll();
        ZipMgt.GetEntries(TempNameValueBuffer);
        if TempNameValueBuffer.FindFirst() then begin
            Clear(TempBlob);
            TempBlob.Blob.CreateOutStream(ResponseOutStream, TextEncoding::UTF8);
            ZipMgt.WriteEntryFromZipToOutStream(TempNameValueBuffer.Name, ResponseOutStream);
            TempBlob.Blob.CreateInStream(ResponseInStream, TextEncoding::UTF8);
            ParseXml(ResponseInStream);
        end;
    end;

I need OpenZipFromStream function to read my Zip from stream to codeunit. I need "Name/Value Buffer" table to read file names inside the archive with GetEntries function. And I need WriteEntryFromZipToOutStream function to read file from archive to stream.

  • After that I could get Xml file from stream and read needed element value:
    local procedure ParseXml(ResponseInStream: InStream);
    var
        XmlDoc: XmlDocument;
        Node: XmlNode;
    begin
        XML := '';
        XmlDocument.ReadFrom(ResponseInStream, XmlDoc);
        if XmlDoc.SelectSingleNode('//' + Element, Node) then
            XML := Node.AsXmlElement().InnerText();
    end;


My codeunit also contains some more functions but they are not so important.

After App publishing you'll see "Zip Reader" page (or find it with TellMe):

You can use your own link and element name, but if you just push "Read Zip" action - you'll get my default values from test example. And result from xml file:

I hope this example will help you to manage Zip archives.

Attention! It looks like "Zip Stream Wrapper" codeunit works only with *.zip archives. I tested it with *.7z file and it failed.