LZ4を使ってみました。
速い。非常に高速な圧縮アルゴリズムです。
GZipとの比較だと3割ほど速く、圧縮率はGZipに3割劣るいった結果でした。
※圧縮対象次第で結果は変わるのでなんとも言えませんが。
速いと嬉しいです。
JSONのデータをDBに格納する際に、圧縮してBinaryで入れてDBサイズ小さくしたり
ネットワーク転送するときに、GZipだと圧縮に時間がかかって結果的に遅くなったりするとこを改善したり
LZ4って
オープンソースで開発されている圧縮アルゴリズムです。
GZipとかと同じものです。C#から使う事ができます。
アーカイバlz4を試してみたら(圧縮は)速すぎ笑った | Nobwak's Lair
lz4 - Extremely Fast Compression algorithm - Google Project Hosting
C#での実装
1.Nugetから取得
2.実装例
あくまでも一例です。
/// <summary> /// Lz4を使用した圧縮/解凍ユーティリティ /// </summary> public static class CompressUtil { /// <summary> /// オブジェクトを圧縮 /// </summary> /// <param name="obj"></param> /// <returns></returns> public static byte[] Compress(object obj) { using (MemoryStream ms = new MemoryStream()) { (new BinaryFormatter()).Serialize(ms, obj); return Compress(ms.ToArray()); } } /// <summary> /// バイナリを解凍しTへ変換して返す /// </summary> /// <typeparam name="T"></typeparam> /// <param name="buffer"></param> /// <returns></returns> public static T Decompress<T>(byte[] buffer) { using (MemoryStream sm = new MemoryStream()) { byte[] oTemp = Decompress(buffer); sm.Write(oTemp, 0, oTemp.Length); sm.Seek(0, System.IO.SeekOrigin.Begin); return (T)(new BinaryFormatter()).Deserialize(sm); } } private static byte[] Compress(byte[] buffer) { using (MemoryStream ms = new MemoryStream()) { using (Lz4Net.Lz4CompressionStream compsrsm = new Lz4Net.Lz4CompressionStream(ms, Lz4Net.Lz4Mode.Fast)) { compsrsm.Write(buffer, 0, buffer.Length); } return ms.ToArray(); } } private static byte[] Decompress(byte[] buffer) { using (MemoryStream outsm = new MemoryStream()) { using (MemoryStream insm = new MemoryStream(buffer, 0, buffer.Length)) { using (Lz4Net.Lz4DecompressionStream compsrsm = new Lz4Net.Lz4DecompressionStream(insm)) { byte[] oTemp = new byte[1024]; int nRead; while ((nRead = compsrsm.Read(oTemp, 0, oTemp.Length)) > 0) { outsm.Write(oTemp, 0, nRead); } } } return outsm.ToArray(); } } }
Newtonsoft.JsonでオブジェクトをJSON形式に変換後にLZ4で圧縮してDBに突っ込んだりしてます。
(∩´∀`)∩ワーイ