1.序列化:
Serialize
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
'压缩传输
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
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
'压缩传输
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.反序列化:
Deserializ
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
'压缩传输
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
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
'压缩传输
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