Click here to Skip to main content
12,816,406 members (28,502 online)
Click here to Skip to main content
Add your own
alternative version

Stats

39.7K views
748 downloads
21 bookmarked
Posted 7 Jun 2007

Zipping files in a custom pipeline

, 2 Jul 2007 CPOL
Rate this:
Please Sign up or sign in to vote.
This article will tell us how to compress and encrypt a zip file in a custom send pipeline.

Introduction

This article will give you an idea of how to write a custom send pipeline component to compress a Zip file and encrypt it with a password.

Background

Usually, in most enterprise applications, we will have several enterprise partners who will receive our data. If the data which is transferred from source A to source B is huge in size and sensitive, then we will have to compress and encrypt the file. By which the data transmission time is reduced. In this article, I am giving an example to compress a single text file in to a Zip file and encrypt that zip file with a given password. You can extend this to compress multiple files. We have a MpasFileValidator (MapsFileIntegrityDecoder) component, which just checks the length of each record in the input file and pass it to a dissassembler. If you don't want it, don't use it.

Using the code

You can download both zip files and compile and run, but to give your own values to the custom send pipeline component properties, follow these steps:

  • Step 1: Download and extract the ZipFileComponent.zip file (custom pipeline component). Open the project and select the project properties and set its assembly output directory path to Program Files\Microsoft BizTalk Server 2006\PipelineComponents\.
  • Step 2: Download and extract FileZippingPOC.zip (BizTalk artifact application), and open the project and select Zipline.btp.
  • Step 3: In the encode stage, select the existing custom component I already dropped and set the properties in the Properties dialog box with your own values.
  • The properties to set:

    • File name: Your absolute file path which has to be zipped.
    • Compression level: 1 to 5 (used to stream compression level); default is 5.
    • Password: The password value which has to be set to the zip file.
  • Step 4: Deploy BizTalk artifacts.
  • Step 5: Set the bindings for the receive port (input file) and send port (output zip file).
  • Step 6: Drop the file in the input folder (where your receive location is bound to the receive port), and you will see the output Zip file at the send port location.

Here are the code part blocks used to develop this pipeline component:

Setting the custom properties is as shown below:

' custom properties
Private _CompressionLevel As String

Public Property CompressionLevel() As String
    Get
        Return _CompressionLevel
    End Get
    Set
        _CompressionLevel = value
    End Set
End Property

Call your function which implements zipping and encrypting in the Execute() method. Here I am calling my zipping and encrypting function Encode(stream) in the Execute method.

Public Function Execute(ByVal pc As Microsoft.BizTalk.Component.Interop.IPipelineContext, _
       ByVal inmsg As Microsoft.BizTalk.Message.Interop.IBaseMessage) _
       As Microsoft.BizTalk.Message.Interop.IBaseMessage _
       Implements Microsoft.BizTalk.Component.Interop.IComponent.Execute
    Try
        If Not inmsg Is Nothing Then
            Dim originalStream As Stream = inmsg.BodyPart.GetOriginalDataStream()
            inmsg.BodyPart.Data = Encode(originalStream)
            pc.ResourceTracker.AddResource(inmsg.BodyPart.Data)
        End If
    Catch ex As Exception
        System.Diagnostics.Debug.WriteLine(_
               "Exception caught in_ABC.BizTalk.PipelineComponents." & _ 
               "PGPEncodeComponent::Execute: " & ex.Message)
    End Try
    'this way, it's a passthrough pipeline component
    Return inmsg
End Function

Here is the Encode function:

Private Function Encode(ByVal inStream As Stream) As Stream
    Dim outStream As Stream = inStream
    Dim inFile As String = Path.GetTempFileName()
    Dim outFile As String = Path.ChangeExtension(inFile, "zip")
    Try
        Dim zipStream As ZipOutputStream = New ZipOutputStream(File.Create(outFile))
        If Not String.IsNullOrEmpty(_Password) Then
            zipStream.Password = _Password
        End If
        Dim compressionLevel As Integer = DEFAULT_COMPRESSIONLEVEL
        If (Not _CompressionLevel Is Nothing) AndAlso (_CompressionLevel <> "") Then
            compressionLevel = Convert.ToInt32(_CompressionLevel)
        End If
        If (compressionLevel < 0) OrElse (compressionLevel > 9) Then
            compressionLevel = DEFAULT_COMPRESSIONLEVEL
        End If
        zipStream.SetLevel(compressionLevel)
        Dim fileName As String
        If (Not _FileName Is Nothing) AndAlso (_FileName <> "") Then
            fileName = _FileName
        Else
            fileName = DEFAULT_FILENAME
        End If
        Dim entry As ZipEntry = New ZipEntry(fileName)
        zipStream.PutNextEntry(entry)

        Dim buffer As Byte() = New Byte(1024) {}
        Dim count As Integer = inStream.Read(buffer, 0, buffer.Length)
        Do While (count <> 0)
            zipStream.Write(buffer, 0, count)
            count = inStream.Read(buffer, 0, buffer.Length)
        Loop
        zipStream.Finish()
        zipStream.Close()

        outStream = ReadFileToMemoryStream(outFile)
    Catch ex As Exception
        System.Diagnostics.Debug.WriteLine(ex)
        Throw ex
    Finally
        If File.Exists(inFile) Then
            File.Delete(inFile)
        End If
        If File.Exists(outFile) Then
            File.Delete(outFile)
        End If
    End Try
    Return outStream
End Function

That's all!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Venky Gundlapally
Architect
United States United States
Venky has 12+ years of IT experience. Currently working as an independent consultant at KPMG. Expertize in C#, WCF, WF, BizTalk.

You may also be interested in...

Pro

Comments and Discussions

 
GeneralThis code appears to be from the book 'Pro BizTalk 2006' Pin
Member 9311586-Apr-10 20:44
memberMember 9311586-Apr-10 20:44 
Questioncompress multiple files Pin
amal17-Sep-09 4:30
memberamal17-Sep-09 4:30 
QuestionDynamic FTP send port Pin
KarenMor27-Mar-08 4:11
memberKarenMor27-Mar-08 4:11 
GeneralError while unzipping the compressed file Pin
sumant madu25-Oct-07 21:48
membersumant madu25-Oct-07 21:48 
Generalfile name with date Pin
Hsi2189819-Oct-07 8:22
memberHsi2189819-Oct-07 8:22 
Generalunzipping Pin
Vidhya Rao16-Oct-07 21:47
memberVidhya Rao16-Oct-07 21:47 
GeneralGetting Error MpasFileInegrityDecoder Pin
DEEPCHAND_KOSTA2-Jul-07 2:38
memberDEEPCHAND_KOSTA2-Jul-07 2:38 
GeneralIt's Gr8 Article Pin
DEEPCHAND_KOSTA2-Jul-07 0:36
memberDEEPCHAND_KOSTA2-Jul-07 0:36 
GeneralMpasFileInegrityDecoder Component Reference Pin
DEEPCHAND_KOSTA2-Jul-07 0:34
memberDEEPCHAND_KOSTA2-Jul-07 0:34 
GeneralRe: MpasFileInegrityDecoder Component Reference Pin
v gundlapally2-Jul-07 4:17
memberv gundlapally2-Jul-07 4:17 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170308.1 | Last Updated 2 Jul 2007
Article Copyright 2007 by Venky Gundlapally
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid