Lukas S.
Lukas S.

Reputation: 1

How do i decompress a String with GZip?

The compression works just fine, and decompression also, but what should i do if the application got closed directly after the String got compressed? How can i decompress it having the String only?

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

Upvotes: 0

Views: 1031

Answers (1)

David Wilson
David Wilson

Reputation: 4439

When the program is closed, the data in your memory stream is lost and not recoverable. You'll need to save the data to a file first.

Upvotes: 1

Related Questions