biztalkbiztalk-2016biztalk-pipelinesionic-zip

BizTalk Server 2016: Receive a zip file and unzip it


I followed the exact same steps given in the following blog, but the output file is still in zip format with increase in file size and file gets corrupted.

https://social.technet.microsoft.com/wiki/contents/articles/20640.biztalk-server-2010-processing-zip-message-having-multiple-type-files.aspx

Please tell me what is going wrong and any other ways to unzip a received file in receive pipeline. This is the code used in pipeline.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.IO;
    using Microsoft.BizTalk.Message.Interop;
    using Microsoft.BizTalk.Component.Interop;
    using Ionic.Zip;

    namespace UnzipMessages
  {
//Attributes of class
[ComponentCategory(CategoryTypes.CATID_PipelineComponent)]
[ComponentCategory(CategoryTypes.CATID_DisassemblingParser)]

 //Generate unique identifier
[System.Runtime.InteropServices.Guid("9ABA4232-1F8E-4a45-B12D-9BE50160464B")]
public class UnzipMessageDisassembler:IBaseComponent,
                     IComponentUI,
                                      IDisassemblerComponent,
                                      IPersistPropertyBag       
                                      
{
    //Implementing Interfaces
    #region IBaseComponent

    private const string description = "UnzipMessages pipeline component";
    private const string name = "UnzipMessageDisaasembler";
    private const string version = "1.0.0.0";
     
    public string Description
    {
        get
        {
            return description;
        }
    }
     
    public string Name
    {
        get
        {
            return name;
        }
    }
     
    public string Version
    {
        get
        {
            return version;
        }
    }

    #endregion


    #region IComponentUI

    private IntPtr icon = new IntPtr();

    public IntPtr Icon
    {
        get
        {
            return icon;
        }
    }

    public System.Collections.IEnumerator Validate(object projectsystem)
    {
        return null;
    }

    #endregion


    #region IPersistPropertyBag
    public void GetClassID(out Guid classid)
    {
        classid = new System.Guid("9ABA4232-1F8E-4a45-B12D-9BE50160464B");
    }

    public void InitNew()
    {
     
    }
    public void Load(IPropertyBag propertyBag, int errorlog)
    {
     
    }
    public void Save(IPropertyBag propertyBag, bool clearDirty, bool saveAllProperties)
    {
     
    }
    #endregion

    #region IDisassemblerComponent
    // This component will read the zipped input message as a stream and with the help 
    // of Zip library the message will unzipped and stored in the Queue.
    private System.Collections.Queue OutFiles = new System.Collections.Queue();

    public void Disassemble(IPipelineContext pContext, IBaseMessage pInMsg)
    {
        IBaseMessagePart msgBodyPart = pInMsg.BodyPart;
        if (msgBodyPart != null)
        {
            Stream msgBodyPartStream = msgBodyPart.GetOriginalDataStream();
            if (msgBodyPartStream != null)
            {
                using (ZipInputStream zipInputStream = new ZipInputStream(msgBodyPartStream))
                {
                    ZipEntry entry = zipInputStream.GetNextEntry();
                    while (entry != null)
                    {
                        MemoryStream memStream = new MemoryStream();
                        byte[] buffer = new Byte[1024];

                        int bytesRead = 1024;
                        while (bytesRead != 0)
                        {
                          bytesRead = zipInputStream.Read(buffer, 0, buffer.Length);
                          memStream.Write(buffer, 0, bytesRead);
                        }

                        //Creating outMessage
                        IBaseMessage outMessage;
                        outMessage = pContext.GetMessageFactory().CreateMessage();
                        outMessage.AddPart("Body", pContext.GetMessageFactory().CreateMessagePart(), true);
                        memStream.Position = 0;
                        outMessage.BodyPart.Data = memStream;

                        //Creating custom context property to hold extension of file
                        string extension = string.Empty;
                        extension = entry.FileName.Substring(entry.FileName.IndexOf("."));
                         
                        //Promoting the custom property
                        pInMsg.Context.Promote("Extension","https://DemoZip.ZipMessageProperties", extension);
                        
                        outMessage.Context = PipelineUtil.CloneMessageContext(pInMsg.Context);
                             
                        //Add outMessage to queue
                        OutFiles.Enqueue(outMessage);

                        entry = zipInputStream.GetNextEntry();
                   }                      
                     
               }
                  
             }
           
          }
         
        }    
   
    public IBaseMessage GetNext(IPipelineContext pContext)
    {
        if (OutFiles.Count > 0)
            return (IBaseMessage)OutFiles.Dequeue();
        else
            return null;
     
    }
    #endregion
}

}

Thanks.


Solution

  • here's another example from github source: https://github.com/BizTalkCommunity/BizTalk-PipelineComponents-Extensions-UtilityPack/blob/master/BTS2016/BizTalk.PipelineComponents.ZipFile/ZipFile.cs