如何使用GZip解压缩字符串?

时间:2016-01-23 11:35:41

标签: vb.net

压缩工作正常,还有解压缩,但是如果应用程序在String被压缩后直接关闭,该怎么办?如何只使用String解压缩它?

//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()

Dim compressed As String = Convert.ToBase64String(mem.ToArray())

//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()

Dim decompressed As String = sr.ReadLine()

1 个答案:

答案 0 :(得分:1)

当程序关闭时,内存流中的数据将丢失且无法恢复。您需要先将数据保存到文件中。