Compressing text

I had a situation today when I had to fit about 12.000 bytes of text in 5.000 byte storage. In .NET, this is so easy to do. Just a few lines of code is required to convert a long String to a short Byte array. Examine the CompressText function. This particular example, compresses 252 characters to 287 bytes, but on larger text pieces, the effect is better. Enjoy!

Module Module1

    Sub Main()

        'For the example, create some text!
        Dim S As New System.Text.StringBuilder()
        S.Append("This is some text that I want to compress. ")
        S.Append("Uncompressed, this text of 252 characters ")
        S.Append("consumes 504 bytes of memory with UTF-8 ")
        S.Append("encoding. The effect of the compression ")
        S.Append("is much better with a larger piece of text. ")
        S.Append("Compressing XML this way is very effective.")

        'Compress it, and display the result.
        Dim B() As Byte = CompressText(S.ToString())
        Console.WriteLine("Compressed to {0} bytes.", B.Length.ToString())

    End Sub

    Private Function CompressText(ByVal T As String) As Byte()
        Dim B() As Byte = System.Text.Encoding.UTF8.GetBytes(T)
        Using MemStream As New System.IO.MemoryStream()
            Using GZStream As New System.IO.Compression.GZipStream(MemStream, _
             IO.Compression.CompressionMode.Compress)
                GZStream.Write(B, 0, B.Length)
                GZStream.Close()
                Return MemStream.ToArray()
            End Using
        End Using
    End Function

End Module

Comments

2 responses to “Compressing text”

  1. […] function, CompressText is unchanged, so I only show the Main […]

  2. […] This post shows how to compress a String to reduce the amount memory it consumes, and this post shows how to use the CompressText function. To be able to read the content of the string, it must be decompressed (or inflated) again. The DecompressText function is one way to do this. […]

Leave a Reply

Your email address will not be published. Required fields are marked *