压缩序列化的类数据

时间:2014-11-03 10:27:43

标签: vb.net memorystream lossless-compression

我有这个类来编码和解码一个序列化的类到一个字符串并返回。

Imports System.IO
Imports System.Runtime.Serialization.Formatters.Binary
Imports System.Runtime.Serialization
Imports System.Text


Public Class FormData


  Public Function Encode(fdi As FormDataItem) As String
    Dim formatter As New BinaryFormatter
    Dim ms As New MemoryStream

    formatter.Serialize(ms, fdi)

    ms.Position = 0

    Dim output As New StringBuilder
    Dim waarde As String = ""

    Do While ms.Position < ms.Length
      waarde = Hex(ms.ReadByte)
      If waarde.Length = 1 Then waarde = "0" + waarde

      output.Append(waarde)

    Loop

    Return output.ToString
  End Function

  Public Function Decode(text As String) As FormDataItem
    Dim ms As New MemoryStream()
    Dim waarde As Byte
    Dim getal As String

    For ab = 0 To text.Length - 1 Step 2
      getal = text.Substring(ab, 2)
      waarde = CByte(Val("&h" + getal))

      ms.WriteByte(waarde)
    Next

    ms.Position = 0

    Dim formatter As New BinaryFormatter
    Dim fdi As New FormDataItem

    ms.Position = 0

    Try
      fdi = DirectCast(formatter.Deserialize(ms), FormDataItem)
    Catch ex As Exception
      Return Nothing
    End Try

    Return fdi
  End Function
End Class

现在我想在encode函数中压缩字符串并在decode函数中解压缩,因为这些数据用在URL中。

我有点卡在压缩数据的位置和方法上。 我是在序列化后立即执行此操作还是只压缩字符串?

任何帮助表示赞赏!

RG。 埃里克

0 个答案:

没有答案