Compressing and decompressing files with BZip2 in .NET C#

BZip2 is yet another data compression algorithm, similar to GZip and Deflate. There’s no native support for BZip2 (de)compression in .NET but there’s a NuGet package provided by icsharpcode.net.

Read more of this post

Advertisements

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.

Read more of this post

Compressing and decompressing files with BZip2 in .NET C#

BZip2 is yet another data compression algorithm, similar to GZip and Deflate. There’s no native support for BZip2 (de)compression in .NET but there’s a NuGet package provided by icsharpcode.net.

Read more of this post

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.

Compressing and decompressing files with BZip2 in .NET C#

BZip2 is yet another data compression algorithm, similar to GZip and Deflate. There’s no native support for BZip2 (de)compression in .NET but there’s a NuGet package provided by icsharpcode.net.

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

sharpziplib nuget

You can compress a file as follows:

FileInfo fileToBeZipped = new FileInfo(@"c:\bzip2\logfile.txt");
FileInfo zipFileName = new FileInfo(string.Concat(fileToBeZipped.FullName, ".bz2"));
using (FileStream fileToBeZippedAsStream = fileToBeZipped.OpenRead())
{
	using (FileStream zipTargetAsStream = zipFileName.Create())
	{
		try
		{
			BZip2.Compress(fileToBeZippedAsStream, zipTargetAsStream, true, 4096);
		}
		catch (Exception ex)
		{
			Console.WriteLine(ex.Message);
		}
	}
}

…and this is how you can decompress the resulting bz2 file again:

using (FileStream fileToDecompressAsStream = zipFileName.OpenRead())
{
	string decompressedFileName = @"c:\bzip2\decompressed.txt";
	using (FileStream decompressedStream = File.Create(decompressedFileName))
	{
		try
		{
			BZip2.Decompress(fileToDecompressAsStream, decompressedStream, true);
		}
		catch (Exception ex)
		{
			Console.WriteLine(ex.Message);
		}
	}
}

Read all posts dedicated to file I/O here.

ultimatemindsettoday

A great WordPress.com site

Elliot Balynn's Blog

A directory of wonderful thoughts

Softwarearchitektur in der Praxis

Wissenswertes zu Webentwicklung, Domain-Driven Design und Microservices

Technology Talks

on Microsoft technologies, Web, Android and others

Software Engineering

Web development

Disparate Opinions

Various tidbits

chsakell's Blog

WEB APPLICATION DEVELOPMENT TUTORIALS WITH OPEN-SOURCE PROJECTS

Guru N Guns's

OneSolution To dOTnET.

Johnny Zraiby

Measuring programming progress by lines of code is like measuring aircraft building progress by weight.

%d bloggers like this: