在自定义管道中压缩文件






1.60/5 (4投票s)
本文将介绍如何在自定义发送管道中压缩和加密 ZIP 文件。
引言
本文将向您展示如何编写自定义发送管道组件,以压缩 ZIP 文件并使用密码对其进行加密。
背景
通常,在大多数企业应用中,我们会有多个企业合作伙伴接收我们的数据。如果从源 A 到源 B 传输的数据量很大且敏感,那么我们就必须压缩和加密文件。这样可以减少数据传输时间。在本文中,我提供了一个示例,将单个文本文件压缩成 ZIP 文件,并使用给定的密码加密该 ZIP 文件。您可以将其扩展为压缩多个文件。我们有一个 MpasFileValidator
(MapsFileIntegrityDecoder
) 组件,它只是检查输入文件中每条记录的长度并将其传递给反汇编器。如果您不需要它,请不要使用它。
使用代码
您可以下载这两个 ZIP 文件并编译运行,但是要为自定义发送管道组件属性提供自己的值,请按照以下步骤操作
- 步骤 1:下载并解压缩 ZipFileComponent.zip 文件(自定义管道组件)。打开项目并选择项目属性,然后将程序集输出目录路径设置为 Program Files\Microsoft BizTalk Server 2006\PipelineComponents\。
- 步骤 2:下载并解压缩 FileZippingPOC.zip(BizTalk 工件应用程序),并打开项目并选择 Zipline.btp。
- 步骤 3:在编码阶段,选择我已放置的现有自定义组件,并在属性对话框中将属性设置为您自己的值。
- 文件名:需要压缩的绝对文件路径。
- 压缩级别:1 到 5(用于流压缩级别);默认值为 5。
- 密码:要设置为 ZIP 文件的密码值。
- 步骤 4:部署 BizTalk 工件。
- 步骤 5:设置接收端口(输入文件)和发送端口(输出 ZIP 文件)的绑定。
- 步骤 6:将文件放到输入文件夹中(您的接收位置绑定到接收端口),您将在发送端口位置看到输出 ZIP 文件。
要设置的属性
以下是用于开发此管道组件的代码块
设置自定义属性如下所示
' custom properties
Private _CompressionLevel As String
Public Property CompressionLevel() As String
Get
Return _CompressionLevel
End Get
Set
_CompressionLevel = value
End Set
End Property
在 Execute()
方法中调用实现压缩和加密的函数。在这里,我在 Execute 方法中调用我的压缩和加密函数 Encode(stream)
。
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
这是 Encode
函数
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
就是这样!