public static void Compress(byte[] dest, ref int destLength, byte[] source) { using (MemoryStream stream = new MemoryStream(dest, true)) { using (ZLIBStream ds = new ZLIBStream(stream, CompressionMode.Compress, true)) { ds.Write(source, 0, source.Length); ds.Flush(); } destLength = (int)stream.Position; } }
public static void ZLibCompress(Stream stream, Stream targetStream, CompressionLevel?level = null) { using (var zs = new ZLIBStream(targetStream, level ?? CompressionLevel.Optimal, true)) { int bytesLeidos = 0; byte[] buffer = new byte[1024]; stream.Seek(0, SeekOrigin.Begin); while ((bytesLeidos = stream.Read(buffer, 0, buffer.Length)) > 0) { zs.Write(buffer, 0, bytesLeidos); } } using (var bw = new BinaryWriter(targetStream)) bw.Write((uint)stream.Length); }