Created
February 24, 2016 17:40
-
-
Save gmcelhanon/36813ac0ff835d5f82ea to your computer and use it in GitHub Desktop.
Compresses and serializes data (using binary formatter) to and from base 64 encoding.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.IO; | |
using System.IO.Compression; | |
using System.Runtime.Serialization.Formatters.Binary; | |
namespace GMac.Serialization | |
{ | |
public static class SerializationHelper | |
{ | |
public static T DeserializeFromCompressedBase64<T>(string base64EncodedString) | |
{ | |
if (base64EncodedString.Contains("\n")) | |
base64EncodedString = string.Join(string.Empty, base64EncodedString); | |
var buffer = Convert.FromBase64String(base64EncodedString); | |
var compressedStream = new MemoryStream(buffer); | |
T model = (T) DeserializeFromStream(compressedStream, true); | |
return model; | |
} | |
public static string SerializeToCompressedBase64<T>(T model) | |
{ | |
var ms = new MemoryStream(); | |
SerializeToStream(model, ms, true); | |
byte[] bytes = ms.ToArray(); | |
string encoded = Convert.ToBase64String(bytes); | |
return encoded; | |
} | |
public static IEnumerable<string> SerializeToCompressedBase64<T>(T model, int maxLineLength) | |
{ | |
string encodedString = SerializeToCompressedBase64(model); | |
var encodedLines = ChunksUpTo(encodedString, 200); | |
return encodedLines; | |
} | |
public static void SerializeToStream(object o, Stream outputStream, bool compress = false) | |
{ | |
MemoryStream serializationStream = new MemoryStream(); | |
var formatter = new BinaryFormatter(); | |
formatter.Serialize(serializationStream, o); | |
// Reset position of memory stream so it can act as an input stream | |
serializationStream.Seek(0, SeekOrigin.Begin); | |
if (compress) | |
CompressStream(serializationStream, outputStream); | |
else | |
serializationStream.CopyTo(outputStream); | |
} | |
public static object DeserializeFromStream(Stream inputStream, bool decompress = false) | |
{ | |
Stream deserInputStream; | |
if (decompress) | |
{ | |
deserInputStream = new MemoryStream(); | |
DecompressStream(inputStream, deserInputStream); | |
deserInputStream.Position = 0; | |
} | |
else | |
{ | |
deserInputStream = inputStream; | |
} | |
var formatter = new BinaryFormatter(); | |
object o = formatter.Deserialize(deserInputStream); | |
return o; | |
} | |
private static void CompressStream(Stream inputStream, Stream outputStream) | |
{ | |
// Compress the stream contents | |
using (var compressionStream = new GZipStream(outputStream, CompressionMode.Compress)) | |
{ | |
inputStream.CopyTo(compressionStream); | |
} | |
} | |
private static void DecompressStream(Stream compressedStream, Stream outputStream) | |
{ | |
// Decompress the stream contents | |
using (var decompressionStream = new GZipStream(compressedStream, CompressionMode.Decompress)) | |
{ | |
decompressionStream.CopyTo(outputStream); | |
} | |
} | |
private static IEnumerable<string> ChunksUpTo(string str, int maxChunkSize) | |
{ | |
for (int i = 0; i < str.Length; i += maxChunkSize) | |
yield return str.Substring(i, Math.Min(maxChunkSize, str.Length - i)); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment