Ionic Zip Library v1.9.1.6
Deflate Method (flush)
Reference ► Ionic.Zlib ► ZlibCodec ► Deflate(FlushType)
Deflate one batch of data.
Declaration Syntax
Parameters
- flush (FlushType)
- whether to flush all data as you deflate. Generally you will want to use Z_NO_FLUSH here, in a series of calls to Deflate(), and then call EndDeflate() to flush everything.
Return Value
Z_OK if all goes well.
Remarks
You must have set InputBuffer and OutputBuffer before calling this method.
Examples
CopyC#
private void DeflateBuffer(CompressionLevel level) { int bufferSize = 1024; byte[] buffer = new byte[bufferSize]; ZlibCodec compressor = new ZlibCodec(); Console.WriteLine("\n============================================"); Console.WriteLine("Size of Buffer to Deflate: {0} bytes.", UncompressedBytes.Length); MemoryStream ms = new MemoryStream(); int rc = compressor.InitializeDeflate(level); compressor.InputBuffer = UncompressedBytes; compressor.NextIn = 0; compressor.AvailableBytesIn = UncompressedBytes.Length; compressor.OutputBuffer = buffer; // pass 1: deflate do { compressor.NextOut = 0; compressor.AvailableBytesOut = buffer.Length; rc = compressor.Deflate(FlushType.None); if (rc != ZlibConstants.Z_OK && rc != ZlibConstants.Z_STREAM_END) throw new Exception("deflating: " + compressor.Message); ms.Write(compressor.OutputBuffer, 0, buffer.Length - compressor.AvailableBytesOut); } while (compressor.AvailableBytesIn > 0 || compressor.AvailableBytesOut == 0); // pass 2: finish and flush do { compressor.NextOut = 0; compressor.AvailableBytesOut = buffer.Length; rc = compressor.Deflate(FlushType.Finish); if (rc != ZlibConstants.Z_STREAM_END && rc != ZlibConstants.Z_OK) throw new Exception("deflating: " + compressor.Message); if (buffer.Length - compressor.AvailableBytesOut > 0) ms.Write(buffer, 0, buffer.Length - compressor.AvailableBytesOut); } while (compressor.AvailableBytesIn > 0 || compressor.AvailableBytesOut == 0); compressor.EndDeflate(); ms.Seek(0, SeekOrigin.Begin); CompressedBytes = new byte[compressor.TotalBytesOut]; ms.Read(CompressedBytes, 0, CompressedBytes.Length); }