Represents a Zlib stream for compression or decompression.

The ZlibStream is a Decorator on a . It adds ZLIB compression or decompression to any stream.

Using this stream, applications can compress or decompress data via stream Read() and Write() operations. Either compresssion or decompression can occur through either reading or writing. The compression format used is ZLIB, which is documented in IETF RFC 1950, "ZLIB Compressed Data Format Specification version 3.3". This implementation of ZLIB always uses DEFLATE as the compression method. (see IETF RFC 1951, "DEFLATE Compressed Data Format Specification version 1.3.")

The ZLIB format allows for varying compression methods, window sizes, and dictionaries. This implementation always uses the DEFLATE compression method, a preset dictionary, and 15 window bits by default.

This class is similar to DeflateStream, except that it adds the RFC1950 header and trailer bytes to a compressed stream when compressing, or expects the RFC1950 header and trailer bytes when decompressing. It is also similar to the GZipStream.

Inheritance: System.IO.Stream
        /// <summary>
        ///   Uncompress a ZLIB-compressed byte array into a single string.
        /// </summary>
        ///
        /// <seealso cref="ZlibStream.CompressString(String)"/>
        /// <seealso cref="ZlibStream.UncompressBuffer(byte[])"/>
        ///
        /// <param name="compressed">
        ///   A buffer containing ZLIB-compressed data.
        /// </param>
        ///
        /// <returns>The uncompressed string</returns>
        public static String UncompressString(byte[] compressed)
        {
            using (var input = new MemoryStream(compressed))
            {
                Stream decompressor =
                    new ZlibStream(input, CompressionMode.Decompress);

                return ZlibBaseStream.UncompressString(compressed, decompressor);
            }
        }
        private static void WriteDataChunks(Stream outputStream, WriteableBitmap writeableBitmap, WriteableBitmapSavePngParameters parameters)
        {
            using (var chunkedStream = new ChunkedStream(MaximumChunkSize, data => WriteChunk(outputStream, PngChunkTypeData, data)))
            {
                using (var zlibStream = new ZlibStream(chunkedStream, CompressionMode.Compress, parameters.CompressionLevel, true))
                {
                    writeableBitmap.Invalidate();

                    var pixels = writeableBitmap.Pixels;
                    var width = writeableBitmap.PixelWidth;
                    var height = writeableBitmap.PixelHeight;
                    var index = 0;

                    var dataRowLength = width * 4;
                    var dataRow = new byte[dataRowLength];

                    for (var y = 0; y < height; y++)
                    {
                        zlibStream.WriteByte(0);

                        for (var x = 0; x < width; x++)
                        {
                            var color = pixels[index++];

                            var alpha = (byte)(color >> 24);

                            int alphaInt = alpha;

                            if (alphaInt == 0)
                            {
                                alphaInt = 1;
                            }

                            alphaInt = (255 << 8) / alphaInt;

                            var dataRowOffset = x * 4;

                            dataRow[dataRowOffset] = (byte)((((color >> 16) & 0xFF) * alphaInt) >> 8);
                            dataRow[dataRowOffset + 1] = (byte)((((color >> 8) & 0xFF) * alphaInt) >> 8);
                            dataRow[dataRowOffset + 2] = (byte)(((color & 0xFF) * alphaInt) >> 8);
                            dataRow[dataRowOffset + 3] = alpha;
                        }

                        zlibStream.Write(dataRow, 0, dataRowLength);
                    }
                }
            }
        }
 /// <summary>
 ///   Compress a string into a byte array using ZLIB.
 /// </summary>
 ///
 /// <remarks>
 ///   Uncompress it with <see cref="ZlibStream.UncompressString(byte[])"/>.
 /// </remarks>
 ///
 /// <seealso cref="ZlibStream.UncompressString(byte[])"/>
 /// <seealso cref="ZlibStream.CompressBuffer(byte[])"/>
 /// <seealso cref="GZipStream.CompressString(string)"/>
 ///
 /// <param name="s">
 ///   A string to compress.  The string will first be encoded
 ///   using UTF8, then compressed.
 /// </param>
 ///
 /// <returns>The string in compressed form</returns>
 public static byte[] CompressString(String s)
 {
     using (var ms = new MemoryStream())
     {
         Stream compressor =
             new ZlibStream(ms, CompressionMode.Compress, CompressionLevel.BestCompression);
         ZlibBaseStream.CompressString(s, compressor);
         return ms.ToArray();
     }
 }