internal static int Process(zlib.ZStream /*!*/ zst, MutableString str, zlib.FlushStrategy flush, bool compress, out MutableString /*!*/ result, ref MutableString trailingUncompressedData) { result = MutableString.CreateBinary(); // add previously compressed data to the output: if (zst.next_out != null) { result.Append(zst.next_out, 0, zst.next_out_index); } int err; int bufferStart = zst.next_out_index; err = Process(zst, str, flush, compress, ref trailingUncompressedData); result.Append(zst.next_out, bufferStart, zst.next_out_index - bufferStart); if (err == Z_STREAM_END && (flush == zlib.FlushStrategy.Z_FINISH || str == null)) { err = compress ? zst.deflateEnd() : zst.inflateEnd(); } zst.next_out = null; zst.next_out_index = 0; zst.avail_out = 0; return(err); }
/// <summary> /// Frees allocated resources. /// </summary> public void End() { z.deflateEnd(); z.free(); z = null; }
public static string compress([BytesConversion]IList<byte> data, [DefaultParameterValue(Z_DEFAULT_COMPRESSION)]int level) { 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"); }