Compressing and decompressing strings with BZip2 in .NET C#

There are times when you need to return a large text from a web service. The large text will then need to be handled by the recipient. In order to reduce the size of the message you can combine two simple techniques:

  • Compress the string value with a compression algorithm, such as BZip2
  • Base64 encode the resulting byte array

You will be able to send the base 64 encoded compressed string over the wire.

You’ll need to import the following NuGet package to use BZip2:

sharpziplib nuget

This is how you can compress a string and base 64 encode it:

string largeUncompressedText = "<root><value size=\"xxl\">This is a large text</value></root>";
string largeCompressedText = string.Empty;
using (MemoryStream source = new MemoryStream(Encoding.UTF8.GetBytes(largeUncompressedText)))
{
	using (MemoryStream target = new MemoryStream())
	{
		BZip2.Compress(source, target, true, 4096);
		byte[] targetByteArray = target.ToArray();
		largeCompressedText = Convert.ToBase64String(targetByteArray);
	}
}

The variable “largeCompressedText” can be sent to a listener who will be able to read it as follows:

byte[] largeCompressedTextAsBytes = Convert.FromBase64String(largeCompressedText);
using (MemoryStream source = new MemoryStream(largeCompressedTextAsBytes))
{
	using (MemoryStream target = new MemoryStream())
	{
		BZip2.Decompress(source, target, true);
		string uncompressedString = Encoding.UTF8.GetString(target.ToArray());
		Console.WriteLine(uncompressedString);
	}
}

The example is not perfect in a sense that largeCompressedText will be bigger than the actual source string but you’ll see the benefits with much larger texts.

View all posts related to string and text operations here.

Advertisement

Concatenate strings with the StringJoiner class in Java 8

Java 8 introduces a new object which enables you to join individual strings: the StringJoiner.

The StringJoiner has two overloads. The simpler one accepts a delimiter:

StringJoiner sj = new StringJoiner(" | ");
sj.add("Hello").add("my").add("dear").add("world!");

System.out.println(sj.toString());

This prints the following:

Hello | my | dear | world!

Note how the StringJoiner was smart enough not put the delimiter after the last string.

In case you don’t need any delimiter then you can just pass in an empty string:

StringJoiner sj = new StringJoiner("");
sj.add("Hello ").add("my").add(" dear").add(" world!");

This will print Hello my dear world! accordingly.

This simpler overload of StringJoiner can be called indirectly using the String.join static method:

String res = String.join(" | ", "Hello", "my", "dear", "world");

The String.join method has another version where you can pass in an iterable class such as an array or array list of strings instead of specifying the elements one by one like above.

The other overload of StringJoiner allows you to specify an opening and ending string to encapsulate the concatenated string:

StringJoiner sj = new StringJoiner(" | ", "-=", "=-");
sj.add("Hello").add("my").add("dear").add("world!");

The result looks as follows:

-=Hello | my | dear | world!=-

We saw an example of Collectors.joining in this post on the Stream API. The joining method uses a StringJoiner behind the scenes.

View all posts related to Java here.

Elliot Balynn's Blog

A directory of wonderful thoughts

Software Engineering

Web development

Disparate Opinions

Various tidbits

chsakell's Blog

WEB APPLICATION DEVELOPMENT TUTORIALS WITH OPEN-SOURCE PROJECTS

Once Upon a Camayoc

Bite-size insight on Cyber Security for the not too technical.

%d bloggers like this: