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); }
public static string decompress([BytesConversion]IList<byte> data, [DefaultParameterValue(MAX_WBITS)]int wbits, [DefaultParameterValue(DEFAULTALLOC)]int bufsize) { byte[] input = data.ToArray(); byte[] outputBuffer = new byte[bufsize]; byte[] output = new byte[bufsize]; int outputOffset = 0; ZStream zst = new ZStream(); zst.next_in = input; zst.avail_in = input.Length; zst.next_out = outputBuffer; zst.avail_out = outputBuffer.Length; int err = zst.inflateInit(wbits); if(err != Z_OK) { zst.inflateEnd(); throw zlib_error(zst, err, "while preparing to decompress data"); } do { err = zst.inflate(FlushStrategy.Z_FINISH); if(err != Z_STREAM_END) { if(err == Z_BUF_ERROR && zst.avail_out > 0) { zst.inflateEnd(); throw zlib_error(zst, err, "while decompressing data"); } else if(err == Z_OK || (err == Z_BUF_ERROR && zst.avail_out == 0)) { // copy to the output and reset the buffer if(outputOffset + outputBuffer.Length > output.Length) Array.Resize(ref output, output.Length * 2); Array.Copy(outputBuffer, 0, output, outputOffset, outputBuffer.Length); outputOffset += outputBuffer.Length; zst.next_out = outputBuffer; zst.avail_out = outputBuffer.Length; zst.next_out_index = 0; } else { zst.inflateEnd(); throw zlib_error(zst, err, "while decompressing data"); } } } while(err != Z_STREAM_END); err = zst.inflateEnd(); if(err != Z_OK) { throw zlib_error(zst, err, "while finishing data decompression"); } if(outputOffset + outputBuffer.Length - zst.avail_out > output.Length) Array.Resize(ref output, output.Length * 2); Array.Copy(outputBuffer, 0, output, outputOffset, outputBuffer.Length - zst.avail_out); outputOffset += outputBuffer.Length - zst.avail_out; return PythonAsciiEncoding.Instance.GetString(output, 0, outputOffset); }
/// <summary> /// Frees allocated resources. /// </summary> public virtual void End() { z.inflateEnd(); z.free(); z = null; }
internal static byte[] Decompress(byte[] input, int wbits=MAX_WBITS, int bufsize=DEFAULTALLOC) { byte[] outputBuffer = new byte[bufsize]; byte[] output = new byte[bufsize]; int outputOffset = 0; ZStream zst = new ZStream(); zst.next_in = input; zst.avail_in = input.Length; zst.next_out = outputBuffer; zst.avail_out = outputBuffer.Length; int err = zst.inflateInit(wbits); if(err != Z_OK) { zst.inflateEnd(); throw zlib_error(zst, err, "while preparing to decompress data"); } do { err = zst.inflate(FlushStrategy.Z_FINISH); if(err != Z_STREAM_END) { if(err == Z_BUF_ERROR && zst.avail_out > 0) { zst.inflateEnd(); throw zlib_error(zst, err, "while decompressing data"); } else if(err == Z_OK || (err == Z_BUF_ERROR && zst.avail_out == 0)) { // copy to the output and reset the buffer if(outputOffset + outputBuffer.Length > output.Length) Array.Resize(ref output, output.Length * 2); Array.Copy(outputBuffer, 0, output, outputOffset, outputBuffer.Length); outputOffset += outputBuffer.Length; zst.next_out = outputBuffer; zst.avail_out = outputBuffer.Length; zst.next_out_index = 0; } else { zst.inflateEnd(); throw zlib_error(zst, err, "while decompressing data"); } } } while(err != Z_STREAM_END); err = zst.inflateEnd(); if(err != Z_OK) { throw zlib_error(zst, err, "while finishing data decompression"); } if(outputOffset + outputBuffer.Length - zst.avail_out > output.Length) Array.Resize(ref output, output.Length * 2); Array.Copy(outputBuffer, 0, output, outputOffset, outputBuffer.Length - zst.avail_out); outputOffset += outputBuffer.Length - zst.avail_out; return output.Take(outputOffset).ToArray(); }