public static void Encode(string original, string encoded, bool high, int block, int chunk) { var flags = high ? RefStreamFlags.HighCompression : RefStreamFlags.None; using (var input = File.OpenRead(original)) using (var encode = new RefStream(File.Create(encoded), RefStreamMode.Compress, flags)) { var buffer = new byte[chunk]; while (true) { var read = input.Read(buffer, 0, buffer.Length); if (read == 0) { break; } encode.Write(buffer, 0, read); } } }
//Json End public void CompressAndSaveLZ4(T obj, string fileName) { using (FileStream fs = new FileStream(fileName, FileMode.OpenOrCreate)) { using (MemoryStream objectSerialization = new MemoryStream()) { var byt = GetBytes(obj); using (BinaryWriter binaryWriter = new BinaryWriter(fs)) { binaryWriter.Write(byt.Length); //write the length first using (var stream = new LZ4.LZ4Stream(fs, LZ4.LZ4StreamMode.Compress, true, byt.Count())) { stream.Write(byt, 0, byt.Length); } } } } }
static void Main(string[] args) { var testCount = 1; var testFile = "test.txt"; GenerateFile(testFile); var testData = File.ReadAllBytes(testFile); int size = 0; ExecuteTest(testCount, "Copy", testData, (data) => { var copy = new byte[data.Length]; data.CopyTo(copy, 0); return(copy.Length); }); ExecuteTest(testCount, "ft.lz4", testData, (data) => { using (var memoryStream = new MemoryStream(64 * 1024)) { using (var lz4 = FT.LZ4.LZ4Stream.Encode(memoryStream)) { lz4.Write(data, 0, data.Length); lz4.Flush(); return((int)memoryStream.Length); } } }); ExecuteTest(testCount, "Gzip", testData, (data) => { using (var memoryStream = new MemoryStream(64 * 1024)) { using (var gz = new GZipStream(memoryStream, CompressionMode.Compress)) { gz.Write(data, 0, data.Length); gz.Flush(); return((int)memoryStream.Length); } } }); ExecuteTest(testCount, "deflate", testData, (data) => { using (var memoryStream = new MemoryStream(64 * 1024)) { using (var deflate = new DeflateStream(memoryStream, CompressionMode.Compress)) { deflate.Write(data, 0, data.Length); deflate.Flush(); return((int)memoryStream.Length); } } }); /* * ExecuteTest(testCount, "k40s", testData, (data) => * { * using (var memoryStream = new MemoryStream(64 * 1024)) * { * using (var lz4 = K4os.Compression.LZ4.Streams.LZ4Stream.Encode(memoryStream)) * { * lz4.Write(data, 0, data.Length); * lz4.Flush(); * return (int)memoryStream.Length; * } * } * }); */ ExecuteTest(testCount, "lz4net", testData, (data) => { using (var memoryStream = new MemoryStream(64 * 1024)) { using (var lz4 = new LZ4.LZ4Stream(memoryStream, CompressionMode.Compress)) { lz4.Write(data, 0, data.Length); lz4.Flush(); return((int)memoryStream.Length); } } }); Console.ReadKey(); }