Logo
programming4us
programming4us
programming4us
programming4us
Home
programming4us
XP
programming4us
Windows Vista
programming4us
Windows 7
programming4us
Windows Azure
programming4us
Windows Server
programming4us
Windows Phone
 
Windows Server

BizTalk 2009 : Dealing with Compressed Files (part 1) - Sending Simple Zipped Files

- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019
7/25/2012 4:37:02 PM
A common problem that most BizTalk projects encounter is having to either send or receive compressed data in the form of a ZIP file. Often, the incoming ZIP file has multiple entries, each of which need to be submitted to BizTalk. In most cases, these files are XML files, but in some situations, they can be either flat files or binary files instead. The examples in the following sections outline a potential simplified solution for sending and receiving zipped information. Additionally, you'll get a chance to explore ways to augment the solution using new functionality within BizTalk.

1. Sending Simple Zipped Files

In order to send a ZIP file from BizTalk, you will need to create a custom send pipeline and a custom encoding pipeline component. The pipeline component will be responsible for examining the incoming message, getting access to the message's data, compressing it, and returning it to BizTalk. The simplest scenario is the "single message in/single message out" scenario. Here, a message is sent to the send pipeline, it is compressed, and a single message is sent out. The pipeline component required for this is documented in the following class:

using System;
using System;
using System.ComponentModel;
using System.Collections;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Reflection;
using Microsoft.BizTalk.Component.Interop;
using ICSharpCode.SharpZipLib.Zip;

    namespace Probiztalk.Samples.PipelineComponents
{
    [ComponentCategory(CategoryTypes.CATID_PipelineComponent)]
    [ComponentCategory(CategoryTypes.CATID_Encoder)]
    [System.Runtime.InteropServices.Guid("56C7B68B-F288-4f78-A67F-20043CA4943E")]
    public class ZipEncodeComponent :
        IBaseComponent,
        Microsoft.BizTalk.Component.Interop.IComponent,
        Microsoft.BizTalk.Component.Interop.IPersistPropertyBag,
        IComponentUI
    {

        // Component information
        #region IBaseComponent
        [Browsable(false)] public string Name
        {
             get
             {
                 return "ZIP encoder";
             }
        }
        [Browsable(false)] public string Version
        {
             get
             {
                return "1.0";
             }
         }
         [Browsable(false)] public string Description
         {
              get
              {
                 return "Zip Encode Pipeline Component";
              }
         }
         [Browsable(false)] public System.IntPtr Icon

					  

{
              get
              {
                 return ((Bitmap)resourceManager.
                                 GetObject("IconBitmap")).
                                 GetHicon();
              }
        }
        #endregion

        private System.Resources.ResourceManager resourceManager =
            new System.Resources.ResourceManager(
                "Probiztalk.Samples.PipelineComponents.ZipDecodeComponent",
                Assembly.GetExecutingAssembly());

        // Property: Password
        private string _password;
        [
        DisplayName("Password"),
         Description("Password used to zip messages.")
         ]
         public string Password
         {
             get { return _password; }
             set { _password = value; }
         }

         // Property: Filename
         private const string DEFAULT_FILENAME = "file";
         private string _filename;
         [
         DisplayName("Filename"),
         Description(@"The name of the file that contains the output message.
                     This file will be added to the ZIP compressed output archive.
                     Default is """ + DEFAULT_FILENAME + @""".")
         ]
         public string Filename
         {
             get { return _filename; }
             set { _filename = value; }
         }
         // Property: CompressionLevel
         const string DEFAULT_COMPRESSIONLEVEL_TEXT = "5";
         const int DEFAULT_COMPRESSIONLEVEL= 5;
         private string _compressionlevel;

					  

[
         DisplayName("CompressionLevel"),
         Description(@"Compression level: 0=Store Only, to 9=Best Compression.
                     Default is '" + DEFAULT_COMPRESSIONLEVEL_TEXT + "'.")
         ]
         public string CompressionLevel
         {
             get { return _compressionlevel; }
             set { _compressionlevel = value; }
         }

         private Stream Encode (Stream inStream)
         {
             Stream outStream = inStream;
             string inFile = Path.GetTempFileName();
             string outFile = Path.ChangeExtension(inFile, "zip");

             try
             {
                  ZipOutputStream zipStream = new ZipOutputStream(
                                            File.Create( outFile )
                                            );

                  // get password, if supplied
                  if ((_password != null) && (_password != ""))
                      zipStream.Password = _password;

                  // get compression level, if supplied
                  int compressionlevel = DEFAULT_COMPRESSIONLEVEL;
                  if ((_compressionlevel != null) && (_compressionlevel != ""))
                      compressionlevel = Convert.ToInt32( _compressionlevel );
                  if ((compressionlevel < ) || (compressionlevel > 9))
                      compressionlevel = DEFAULT_COMPRESSIONLEVEL;
                  zipStream.SetLevel( compressionlevel );

                  // get message filename, if supplied
                  string filename = (((_filename != null) && (_filename != "")) ?
                                      _filename : DEFAULT_FILENAME );
                  ZipEntry entry = new ZipEntry( filename );
                  zipStream.PutNextEntry( entry );

                  // copy the input into the compressed output stream
                  byte[] buffer = new byte[4096];
                  int count = 0;

					  

while ((count = inStream.Read(buffer, 0, buffer.Length)) != 0)
                      zipStream.Write( buffer, 0, count );
                  zipStream.Finish();
                  zipStream.Close();

                  outStream = Probiztalk.Samples.PipelineUtilities.
                      FileStreamReadWrite.ReadFileToMemoryStream(outFile);
             }
             catch (Exception ex)
             {
                 System.Diagnostics.Debug.WriteLine(ex);
             }
             finally
             {
                 if (File.Exists(inFile))
                 {
                     File.Delete(inFile);
                 }
                 if (File.Exists(outFile))
                 {
                     File.Delete(outFile);
                 }
             }

             return outStream;
         }

         #region IPersistPropertyBag Members

         public void InitNew()
         {
         }

         public void GetClassID(out Guid classID)
         {
             classID = new Guid ("0F94CF83-0B04-49a6-B73C-70473E0CF96F");
         }

         public void Load(IPropertyBag propertyBag, int errorLog)
         {

             string text;
             text = (string)ReadPropertyBag( propertyBag, "Password" );
             if (text != null) _password = text;
             text = (string)ReadPropertyBag( propertyBag, "Filename" );

					  

if (text != null) _filename = text;
             text = (string)ReadPropertyBag( propertyBag, "CompressionLevel" );
             if (text != null) _compressionlevel = text;
         }

         public void Save(IPropertyBag propertyBag, bool clearDirty,
                          bool saveAllProperties)
         {
             object val;
             val = (object)_password;
             WritePropertyBag( propertyBag, "Password", val );
             val = (object)_filename;
             WritePropertyBag( propertyBag, "Filename", val );
             val = (object)_compressionlevel;
             WritePropertyBag( propertyBag, "CompressionLevel", val );
         }

         #endregion


         #region IComponent Members

         public Microsoft.BizTalk.Message.Interop.IBaseMessage Execute
                             (IPipelineContext pContext,
                             Microsoft.BizTalk.Message.Interop.IBaseMessage pInMsg)

         {
             try
             {
                 if (pInMsg != null)
                 {
                     Stream originalStream =
                         pInMsg.BodyPart.GetOriginalDataStream();
                     pInMsg.BodyPart.Data = Encode( originalStream );
                     pContext.ResourceTracker.AddResource( pInMsg.BodyPart.Data );
                 }
             }
             catch (Exception ex)
             {
                 System.Diagnostics.Debug.WriteLine(
                     "Exception caught in ZipEncodeComponent::Execute: " +
                     ex.Message );
             }
             return pInMsg;
         }

					  

#endregion

         #region IComponentUI Members

         /// <summary>
         /// The Validate method is called by the BizTalk Editor during the build
         /// of a BizTalk project.
         /// "<\summary>
         /// <param name="obj">An Object containing the configuration
         /// properties."<\param>
         /// <returns>The IEnumerator enables the caller to enumerate
         /// through a collection of strings containing error messages.
         /// These error messages appear as compiler error messages.
         /// To report successful property validation, the method should return
         /// an empty enumerator."<\returns>
         public IEnumerator Validate(object projectSystem)
         {
             // example implementation:
             // ArrayList errorList = new ArrayList();
             // errorList.Add("This is a compiler error");
             // return errorList.GetEnumerator();
             return null;
         }
         #endregion
         #region Utility functions
         /// <summary>
         /// Reads property value from property bag.
         /// "<\summary>
         /// <param name="pb">Property bag."<\param>
         /// <param name="propName">Name of property."<\param>
         /// <returns>Value of the property."<\returns>
         public static object
             ReadPropertyBag(Microsoft.BizTalk.Component.Interop.IPropertyBag pb,
                             string propName)
         {
             object val = null;

             try
             {
                 pb.Read(propName, out val, 0);
             }
             catch (ArgumentException)
             {
                 return val;
             }
             catch (Exception ex)

					  

{
                 throw new ApplicationException(ex.Message);
             }

             return val;
         }

         /// <summary>
         /// Writes property values into a property bag.
         /// "<\summary>
         /// <param name="pb">Property bag."<\param>
         /// <param name="propName">Name of property."<\param>
         /// <param name="val">Value of property."<\param>
         public static void WritePropertyBag(
                             Microsoft.BizTalk.Component.Interop.IPropertyBag pb,
                             string propName, object val)
         {
             try
             {
                 pb.Write(propName, ref val);
             }
             catch (Exception ex)
             {
                 throw new ApplicationException(ex.Message);
             }
         }


         #endregion
     }
}

					  

2. Sending Multiple Zipped Files

In order to accommodate the scenario where a single ZIP file will contain multiple ZIP entries, you need to make some modifications to the preceding example. Specifically, you need to pass in multiple documents and have them all included within the outbound ZIP file. For this, you can write a BizTalk Assembler component. The Assembler component will take an array of messages, loop through them, and compress each of them into the final message output. To actually send the messages to the assembler, you can use the ExecuteSendPipeline from an orchestration feature. What you need to do is build an orchestration that will have a subscription for all messages required to be included within the ZIP file. The orchestration listens for these messages, and when it receives them, it adds them to an ArrayList object. At some point, the orchestration will need to call the send pipeline and pass it in the ArrayList it has built. From here, the send pipeline will call the Assembler component, which will add each of the messages it has received within the ArrayList to the outbound message. An example of this pattern is included in the SDK with BizTalk under the \Program Files\Microsoft BizTalk Server 2009\ SDK\Samples\Pipelines\Aggregator\directory. 
Other -----------------
- Windows Server 2008 Server Core : Managing Removable Storage with the RSM Utility (part 3) - VIEW, REFRESH, INVENTORY
- Windows Server 2008 Server Core : Managing Removable Storage with the RSM Utility (part 2) - DISMOUNT, EJECT, CREATEPOOL
- Windows Server 2008 Server Core : Managing Removable Storage with the RSM Utility (part 1) - ALLOCATE, DEALLOCATE, MOUNT
- Manage the Active Directory Domain Services Schema : Index Attributes, Remove Attributes from the Index
- Backing Up the Exchange Server 2007 Environment : Backing Up Specific Windows Services
- Backing Up the Exchange Server 2007 Environment : Backing Up the Windows Server 2003 and Exchange Server 2007
- Windows Server 2008 Server Core : Setting and Viewing Application Paths with the Path Command
- Windows Server 2008 Server Core : Creating Symbolic Links and Hard Links with the MKLink Command, Mounting a Volume with the MountVol Utility
- Windows Server 2008 R2 delta changes : Active Directory Changes
- Windows Server 2008 R2 delta changes : Networking Changes
 
 
Top 10
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
 
programming4us
Windows Vista
programming4us
Windows 7
programming4us
Windows Azure
programming4us
Windows Server