Imports System.IO
Imports System.Security.Cryptography
Imports System.Text
Imports System
Imports System.IO.Compression
Public Class Form1
Public Shared Function Compress(ByVal text As String) As String
Dim buffer() As Byte = Encoding.UTF8.GetBytes(text)
Dim memoryStream = New MemoryStream()
Using gZipStream = New GZipStream(memoryStream, CompressionMode.Compress, True)
gZipStream.Write(buffer, 0, buffer.Length)
End Using
memoryStream.Position = 0
Dim compressedData = New Byte(memoryStream.Length - 1) {}
memoryStream.Read(compressedData, 0, compressedData.Length)
Dim gZipBuffer = New Byte(compressedData.Length + 4 - 1) {}
System.Buffer.BlockCopy(compressedData, 0, gZipBuffer, 4, compressedData.Length)
System.Buffer.BlockCopy(BitConverter.GetBytes(buffer.Length), 0, gZipBuffer, 0, 4)
Return Convert.ToBase64String(gZipBuffer)
End Function
Public Shared Function DecompressString(ByVal compressedText As String) As String
Dim gZipBuffer() As Byte = Convert.FromBase64String(compressedText)
Using memoryStream = New MemoryStream()
Dim dataLength As Integer = BitConverter.ToInt32(gZipBuffer, 0)
memoryStream.Write(gZipBuffer, 4, gZipBuffer.Length - 4)
Dim buffer = New Byte(dataLength - 1) {}
memoryStream.Position = 0
Using gZipStream = New GZipStream(memoryStream, CompressionMode.Decompress)
gZipStream.Read(buffer, 0, buffer.Length)
End Using
Return Encoding.UTF8.GetString(buffer)
End Using
End Function
End Class