我需要一个vb.net函数来压缩/解压缩字符串,并将结果作为字符串返回。我发现了这两个功能:
//Compress
Dim mem As New IO.MemoryStream
Dim gz As New System.IO.Compression.GZipStream(mem, IO.Compression.CompressionMode.Compress)
Dim sw As New IO.StreamWriter(gz)
sw.WriteLine("hello compression")
sw.Close()
//Decompress
Dim mem2 As New IO.MemoryStream(mem.ToArray)
gz = New System.IO.Compression.GZipStream(mem2, IO.Compression.CompressionMode.Decompress)
Dim sr As New IO.StreamReader(gz)
MsgBox(sr.ReadLine)
sr.Close()
但是如何让它们将压缩字符串作为字符串返回?感谢。
答案 0 :(得分:3)
您需要对压缩的字节数组进行Base64编码才能获得字符串表示。
Dim compressed As String = Convert.ToBase64String(mem.ToArray())
解压缩的字符串只能从StreamReader
中读取。
Dim decompressed As String = sr.ReadLine()