The .NET Framework provides compression in System.IO.Compression. The GZipStream is used to compress data. We use VB.NET code to compress a string to a file.
Imports System.IO
Imports System.IO.Compression
Imports System.Text
Module Module1
Sub Main()
' Byte array from string.
Dim array() As Byte = Encoding.ASCII.GetBytes(New String("X"c, 10000))
' Call Compress.
Dim c() As Byte = Compress(array)
' Write bytes.
File.WriteAllBytes("C:\\compress.gz", c)
End Sub
Function Compress(ByVal raw() As Byte) As Byte()
' Clean up memory with Using-statements.
Using memory As MemoryStream = New MemoryStream()
' Create compression stream.
Using gzip As GZipStream = New GZipStream(memory, CompressionMode.Compress, True)
' Write.
gzip.Write(raw, 0, raw.Length)
End Using
' Return array.
Return memory.ToArray()
End Using
End Function
End Module