WebService压缩传输数据 For .NET

1.序列化:  

ContractedBlock.gif ExpandedBlockStart.gif Serialize
ExpandedBlockStart.gifContractedBlock.gifPublic Shared Function Serialize()Function Serialize(ByVal obj As ObjectAs Byte()
        
'不压缩传输
        'Dim objMS As New MemoryStream 
        'Dim objBF As New BinaryFormatter
        'Try
        '    objBF.Serialize(objMS, obj)
        '    Return objMS.GetBuffer()
        'Catch ex As Exception
        '    Return New Byte() {}
        'Finally
        '    objMS.Dispose()
        '    objMS = Nothing
        'End Try

        
'压缩传输
        Dim objMS As New MemoryStream
        
Try
            
If obj Is Nothing Then
                
Return New Byte() {}
            
End If

            
Dim byt As Byte() = Nothing
            
Dim objBF As New BinaryFormatter
            objBF.Serialize(objMS, obj)
            byt 
= objMS.GetBuffer()

            objMS 
= New MemoryStream()
            
Dim gzip As New GZipStream(objMS, CompressionMode.Compress, True)
            gzip.Write(byt, 
0, byt.Length)
            gzip.Close()
            
Return objMS.ToArray()
        
Catch ex As Exception
            
Return New Byte() {}
        
Finally
            objMS.Close()
        
End Try
    
End Function

 

2.反序列化:

ContractedBlock.gif ExpandedBlockStart.gif Deserializ
ExpandedBlockStart.gifContractedBlock.gifPublic Shared Function Deserialize()Function Deserialize(ByVal obj As Byte()) As Object
        
'不压缩传输
        'Dim objMS As New MemoryStream(obj)
        'Dim objBF As New BinaryFormatter
        'Try
        '    Return objBF.Deserialize(objMS)
        'Finally
        '    objMS.Close()
        'End Try

        
'压缩传输
        Dim input As New MemoryStream()
        
Dim output As New MemoryStream()
        
Try
            
If obj Is Nothing Then
                
Return New Object()
            
End If
            
input.Write(obj, 0, obj.Length)
            
Input.Position = 0
            
Dim gzip As New GZipStream(Input, CompressionMode.Decompress, True)

            
Dim buff As Byte() = New Byte(1024) {}
            
Dim read As Integer = -1
            read 
= gzip.Read(buff, 0, buff.Length)
            
While read > 0
                output.Write(buff, 
0, read)
                read 
= gzip.Read(buff, 0, buff.Length)
            
End While
            gzip.Close()

            
Dim bf As New BinaryFormatter()
            
Dim buffer As Byte() = output.ToArray()
            output 
= New MemoryStream(buffer)
            
Return bf.Deserialize(output)
        
Catch ex As Exception
            
Return New Object()
        
Finally
            
input.Close()
            output.Close()
        
End Try
    
End Function

转载于:https://www.cnblogs.com/yongwnet/archive/2008/10/20/1315525.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值