public static Bytes compress([NotNone] IBufferProtocol data, int level = Z_DEFAULT_COMPRESSION) { using var buffer = data.GetBuffer(); byte[] input = buffer.AsUnsafeArray() ?? buffer.ToArray(); byte[] output = new byte[input.Length + input.Length / 1000 + 12 + 1]; ZStream zst = new ZStream(); zst.next_in = input; zst.avail_in = input.Length; zst.next_out = output; zst.avail_out = output.Length; int err = zst.DeflateInit(level); switch (err) { case (Z_OK): break; case (Z_STREAM_ERROR): throw PythonOps.CreateThrowable(error, "Bad compression level"); default: zst.deflateEnd(); zlib_error(zst, err, "while compressing data"); return(null); } err = zst.deflate(FlushStrategy.Z_FINISH); if (err != Z_STREAM_END) { zst.deflateEnd(); throw zlib_error(zst, err, "while compressing data"); } err = zst.deflateEnd(); if (err == Z_OK) { var res = new byte[(int)zst.total_out]; Array.Copy(output, res, res.Length); return(Bytes.Make(res)); } throw zlib_error(zst, err, "while finishing compression"); }
public static string compress([BytesConversion] IList <byte> data, int level = Z_DEFAULT_COMPRESSION) { byte[] input = data.ToArray(); byte[] output = new byte[input.Length + input.Length / 1000 + 12 + 1]; ZStream zst = new ZStream(); zst.next_in = input; zst.avail_in = input.Length; zst.next_out = output; zst.avail_out = output.Length; int err = zst.DeflateInit(level); switch (err) { case (Z_OK): break; case (Z_STREAM_ERROR): throw PythonOps.CreateThrowable(error, "Bad compression level"); default: zst.deflateEnd(); zlib_error(zst, err, "while compressing data"); return(null); } err = zst.deflate(FlushStrategy.Z_FINISH); if (err != Z_STREAM_END) { zst.deflateEnd(); throw zlib_error(zst, err, "while compressing data"); } err = zst.deflateEnd(); if (err == Z_OK) { return(PythonAsciiEncoding.Instance.GetString(output, 0, (int)zst.total_out)); } else { throw zlib_error(zst, err, "while finishing compression"); } }
internal Compress(int level, int method, int wbits, int memlevel, int strategy) { zst = new ZStream(); int err = zst.DeflateInit(level, wbits); switch (err) { case ZlibModule.Z_OK: break; case ZlibModule.Z_STREAM_ERROR: throw PythonOps.ValueError("Invalid initialization option"); default: throw ZlibModule.zlib_error(this.zst, err, "while creating compression object"); } }
public override void Init(int type, int level) { if (type == DEFLATER) { stream.DeflateInit(level); this.type = DEFLATER; } else { if (type == INFLATER) { stream.InflateInit(); inflated_buf = new byte[BUF_SIZE]; this.type = INFLATER; } } }