I am wanting to compress results from QUERYS of the database before adding them to the cache.
I want to be able to compress any reference type.
I have a working
Here is how this can be done.
GzipStream: Provides methods and properties used to compress and decompress streams.
Serialize object to Memory stream using binaryformatter and hook that memorystream to Gzipstream.
Code to compress is as follows:
public static byte[] ObjectToCompressedByteArray(object obj)
{
try
{
using (var memoryStream = new System.IO.MemoryStream())
{
using (var gZipStream = new GZipStream(memoryStream, CompressionMode.Compress))
{
var binaryFormatter = new BinaryFormatter();
binaryFormatter.Serialize(gZipStream, obj);
}
return memoryStream.ToArray();
}
}
catch (Exception ex)
{
LoggerWrapper.CMLogger.LogMessage(
$"EXCEPTION: BSExportImportHelper.ObjectToByteArray - : {ex.Message}", LoggerWrapper.CMLogger.CMLogLevel.Error);
throw;
}
}