1.序列化:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Serialize
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
Public Shared Function Serialize()Function Serialize(ByVal obj As Object) As 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
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
'压缩传输
Dim objMS As New MemoryStream
Try
If obj Is Nothing Then
Return New Byte() {}
End If
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Dim byt As Byte() = Nothing
Dim objBF As New BinaryFormatter
objBF.Serialize(objMS, obj)
byt = objMS.GetBuffer()
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
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.反序列化:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Deserializ
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
Public 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
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
'压缩传输
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)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
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()
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
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
posted @
2008-10-20 23:13
yongwnet
阅读(
793)
评论()
编辑
收藏
举报