WCF Message Compression - Gzip, 7zip

I was using Gzip Encoder to compress wcf message, it surprised me that sometimes the compression message size is even bigger than the original size, so I looked the code, I found within GZipMessageEncoderFactory.cs, the method "CompressBuffer" in the GZipMessageEncoderFactory class is not quite right. it was like this originally:

private static ArraySegment CompressBuffer(ArraySegment buffer, BufferManager bufferManager, int messageOffset)
{
   ....
  var byteArray = new ArraySegment(bufferedBytes, messageOffset,
                                                       bufferedBytes.Length - messageOffset);

  return byteArray;
}

if we use the method above, the count of the new ArraySegment will be the bufferedBytes, as BufferManager using TakeBuffer method to grab a buffer which is the nearest bigger 2N byte, e.g. if input param is 522 bytes for TakeBuffer method, it will grab a buffer which has 1024 bytes. And the constructor of ArraySegment will take the third parameter(bold above) to reflects the actual size of the ArraySegment, if size of the bufferedBytes(first params) is less than third parameter, it will fill byte0 to the rest. that is why sometimes the size is even bigger than the original size. 

it should be like this:

private static ArraySegment CompressBuffer(ArraySegment buffer, BufferManager bufferManager, int messageOffset)
{
   ....
  var byteArray = new ArraySegment(bufferedBytes, messageOffset,
                       compressedBytes.Length);

  return byteArray;
}

Also I found the Gzip compression doesn't have high compression ratio, I decided to use 7zip and gzip combination then, I found a good article about 7zip: 7Zip (LZMA) In-Memory Compression with C#, after adding it to my project, I can use it straight forward:

private static ArraySegment CompressBuffer(ArraySegment buffer, BufferManager bufferManager, int messageOffset)
{
 using (var memoryStream = new MemoryStream())
 {
  var zipperStream = new GZipStream(memoryStream, CompressionMode.Compress, true);

  using (zipperStream)
   zipperStream.Write(buffer.Array,
      buffer.Offset, buffer.Count);

  byte[] compressedBytes1 = memoryStream.ToArray();
  byte[] compressedBytes = SevenZip.Compression.LZMA.SevenZipHelper.Compress(compressedBytes1);
  byte[] bufferedBytes = bufferManager.TakeBuffer(
     compressedBytes.Length + messageOffset);

  Array.Copy(compressedBytes, 0, bufferedBytes, messageOffset,
     compressedBytes.Length);

  bufferManager.ReturnBuffer(buffer.Array);
  var byteArray = new ArraySegment(bufferedBytes, messageOffset,
     compressedBytes.Length);

  return byteArray;
 }
}

//Helper method to decompress an array of bytes
private static ArraySegment DecompressBuffer(ArraySegment buffer, BufferManager bufferManager)
{
 var memoryStream1 = new MemoryStream(buffer.Array, buffer.Offset, buffer.Count - buffer.Offset);
 var memoryStream=new MemoryStream(SevenZip.Compression.LZMA.SevenZipHelper.Decompress(memoryStream1.ToArray()));

 var decompressedStream = new MemoryStream();
 int totalRead = 0;
 int blockSize = 1024;
 byte[] tempBuffer = bufferManager.TakeBuffer(blockSize);
 using (var gzStream = new GZipStream(memoryStream, CompressionMode.Decompress))
 {
  while (true)
  {
   int bytesRead = gzStream.Read(tempBuffer, 0, blockSize);
   if (bytesRead == 0)
    break;
   decompressedStream.Write(tempBuffer, 0, bytesRead);
   totalRead += bytesRead;
  }
 }
 bufferManager.ReturnBuffer(tempBuffer);
  
 byte[] decompressedBytes = decompressedStream.ToArray();
 byte[] bufferManagerBuffer = bufferManager.TakeBuffer(decompressedBytes.Length + buffer.Offset);
 Array.Copy(buffer.Array, 0, bufferManagerBuffer, 0, buffer.Offset);
 Array.Copy(decompressedBytes, 0, bufferManagerBuffer, buffer.Offset, decompressedBytes.Length);

 var byteArray = new ArraySegment(bufferManagerBuffer, buffer.Offset, decompressedBytes.Length);
 bufferManager.ReturnBuffer(buffer.Array);

 return byteArray;
}

This article is part of the GWB Archives. Original Author: argot

New on Geeks with Blogs

  • We Won The One Award I Actually Care About

    Full Scale made the Inc. 5000 for the fifth year straight, the 12th listing across my three companies. Here is why the one award you cannot buy is worth stopping for.

  • Your Customers Build the Features Now

    I let a tool I liked sit dead for a year rather than build the features I wanted. An MCP server meant I never had to, and your customers can do the same to your product.

  • Get the Size of a Directory in Linux the Easy Way

    du -sh for the quick answer, ncdu for the cleanup, df for the disk itself: every command for checking directory size in Linux, plus why du and df never agree.

  • Vim Search and Replace: The Ultimate Guide

    One :%s command replaces every match in a file before a find dialog would even open. The Vim substitute patterns worth the muscle memory: flags, ranges, capture groups, and multi-file edits.