示例#1
0
        public static byte[] Compress(byte[] file)
        {
            int splitSize      = 0x10000;
            int partitionCount = ((file.Length - 1) / 0x10000) + 1;

            var partitionList = new List <byte[]>();

            using (var reader = new BinaryReader(new MemoryStream(file)))
            {
                for (int i = 0; i < partitionCount; i++)
                {
                    var encoding = new ZlibCodec();
                    encoding.InitializeDeflate(Ionic.Zlib.CompressionLevel.BestCompression);    // RFC1950/1951/1952 encoding needed
                    byte[] buffer = ZlibStream.CompressBuffer(reader.ReadBytes(splitSize));
                    encoding.EndDeflate();
                    partitionList.Add(buffer);
                }
            }
            using (var ms = new MemoryStream())
                using (var br = new BinaryWriter(ms))
                {
                    // Header
                    br.Write(splitSize);
                    br.Write(partitionCount);
                    br.Write(file.Length); // Uncompressed file length
                    for (int i = 0; i < partitionList.Count; i++)
                    {
                        br.Write(partitionList[i].Length + 4); // Entry size (partition size member + partition)
                    }
                    long currentOffset = br.BaseStream.Position;
                    // Entries
                    for (int i = 0; i < partitionList.Count; i++)
                    {
                        br.BaseStream.Position = currentOffset + 0x7F & ~0x7F; // Aligning
                        br.Write(partitionList[i].Length);
                        br.BaseStream.Write(partitionList[i], 0x0, partitionList[i].Length);
                        currentOffset = br.BaseStream.Position;
                    }
                    //Align Final data to 128
                    while (br.BaseStream.Position % 128 != 0)
                    {
                        br.Write((byte)0);
                    }
                    return(ms.ToArray());
                }
        }
        private byte[] ZlibCodecCompress(string textToCompress)
        {
            int outputSize = 2048;

            byte[] output           = new Byte[outputSize];
            byte[] uncompressed     = UTF8Encoding.UTF8.GetBytes(textToCompress);
            int    lengthToCompress = uncompressed.Length;

            // If you want a ZLIB stream, set this to true.  If you want
            // a bare DEFLATE stream, set this to false.
            bool wantRfc1950Header = false;

            using (MemoryStream ms = new MemoryStream())
            {
                ZlibCodec compressor = new ZlibCodec();
                compressor.InitializeDeflate(CompressionLevel.BestCompression, wantRfc1950Header);

                compressor.InputBuffer      = uncompressed;
                compressor.AvailableBytesIn = lengthToCompress;
                compressor.NextIn           = 0;
                compressor.OutputBuffer     = output;

                foreach (var f in new FlushType[] { FlushType.None, FlushType.Finish })
                {
                    int bytesToWrite = 0;
                    do
                    {
                        compressor.AvailableBytesOut = outputSize;
                        compressor.NextOut           = 0;
                        compressor.Deflate(f);

                        bytesToWrite = outputSize - compressor.AvailableBytesOut;
                        if (bytesToWrite > 0)
                        {
                            ms.Write(output, 0, bytesToWrite);
                        }
                    }while ((f == FlushType.None && (compressor.AvailableBytesIn != 0 || compressor.AvailableBytesOut == 0)) ||
                            (f == FlushType.Finish && bytesToWrite != 0));
                }

                compressor.EndDeflate();

                ms.Flush();
                return(ms.ToArray());
            }
        }
示例#3
0
    private static byte[] ZlibCodecCompress(byte[] data)
    {
        int buffer_size = 0x800;

        byte[] buffer = new byte[buffer_size];
        int    length = data.Length;
        bool   flag   = false;

        using (MemoryStream stream = new MemoryStream())
        {
            ZlibCodec codec = new ZlibCodec();
            codec.InitializeDeflate(CompressionLevel.Default, flag);
            codec.WindowBits       = -15;
            codec.Strategy         = CompressionStrategy.Default;
            codec.InputBuffer      = data;
            codec.AvailableBytesIn = length;
            codec.NextIn           = 0;
            codec.OutputBuffer     = buffer;
            FlushType[] typeArray1 = new FlushType[2];
            typeArray1[1] = FlushType.Finish;
            foreach (FlushType type in typeArray1)
            {
                int count = 0;
                do
                {
                    codec.AvailableBytesOut = buffer_size;
                    codec.NextOut           = 0;
                    codec.Deflate(type);
                    count = buffer_size - codec.AvailableBytesOut;
                    if (count > 0)
                    {
                        stream.Write(buffer, 0, count);
                    }
                } while (((type == FlushType.None) &&
                          ((codec.AvailableBytesIn != 0) || (codec.AvailableBytesOut == 0))) ||
                         ((type == FlushType.Finish) && (count != 0)));
            }

            codec.EndDeflate();
            stream.Flush();
            return(stream.ToArray());
        }
    }
示例#4
0
        public static byte[] Deflate(byte[] input)
        {
            int outputSize = 8192;

            byte[] output           = new Byte[outputSize];
            int    lengthToCompress = input.Length;

            using (MemoryStream ms = new MemoryStream())
            {
                ZlibCodec compressor = new ZlibCodec();
                compressor.InitializeDeflate(Ionic.Zlib.CompressionLevel.BestCompression, true);

                compressor.InputBuffer      = input;
                compressor.AvailableBytesIn = lengthToCompress;
                compressor.NextIn           = 0;
                compressor.OutputBuffer     = output;

                foreach (var f in new FlushType[] { FlushType.None, FlushType.Finish })
                {
                    int bytesToWrite = 0;
                    do
                    {
                        compressor.AvailableBytesOut = outputSize;
                        compressor.NextOut           = 0;
                        compressor.Deflate(f);

                        bytesToWrite = outputSize - compressor.AvailableBytesOut;
                        if (bytesToWrite > 0)
                        {
                            ms.Write(output, 0, bytesToWrite);
                        }
                    }while (bytesToWrite != 0);
                }

                compressor.EndDeflate();

                ms.Flush();
                return(ms.ToArray());
            }
        }
示例#5
0
    public static byte[] Deflate(byte[] decompressed)
    {
        int       rc;
        ZlibCodec compressor = new ZlibCodec();

        byte[] compressed = new byte[decompressed.Length * 2 + 16];

        rc = compressor.InitializeDeflate(Zlib.CompressionLevel.BestCompression);
        if (rc != ZlibConstants.Z_OK)
        {
            throw new Exception("inflating: " + compressor.Message);
        }

        compressor.InputBuffer      = decompressed;
        compressor.NextIn           = 0;
        compressor.AvailableBytesIn = decompressed.Length;

        compressor.OutputBuffer      = compressed;
        compressor.NextOut           = 0;
        compressor.AvailableBytesOut = compressed.Length;

        while (compressor.TotalBytesIn != decompressed.Length && compressor.TotalBytesOut < compressed.Length)
        {
            compressor.Deflate(FlushType.None);
        }

        do
        {
            rc = compressor.Deflate(FlushType.Finish);
        } while (rc != ZlibConstants.Z_STREAM_END);

        long compressedLen = compressor.TotalBytesOut;

        compressor.EndDeflate();

        byte[] result = new byte[compressedLen];
        Array.Copy(compressed, result, compressedLen);
        return(result);
    }
示例#6
0
        public byte[] Compress(byte[] data)
        {
            var buffer = new byte[4096];
            var zc     = new ZlibCodec(CompressionMode.Compress);

            zc.InitializeDeflate(CompressionLevel.Default, 15, true);

            zc.InputBuffer      = data;
            zc.NextIn           = 0;
            zc.AvailableBytesIn = data.Length;
            zc.OutputBuffer     = buffer;

            using (var ms = new MemoryStream())
            {
                do
                {
                    zc.NextOut           = 0;
                    zc.AvailableBytesOut = buffer.Length;
                    zc.Deflate(FlushType.None);

                    ms.Write(zc.OutputBuffer, 0, buffer.Length - zc.AvailableBytesOut);
                }while (zc.AvailableBytesIn > 0 || zc.AvailableBytesOut == 0);

                do
                {
                    zc.NextOut           = 0;
                    zc.AvailableBytesOut = buffer.Length;
                    zc.Deflate(FlushType.Finish);

                    if (buffer.Length - zc.AvailableBytesOut > 0)
                    {
                        ms.Write(buffer, 0, buffer.Length - zc.AvailableBytesOut);
                    }
                }while (zc.AvailableBytesIn > 0 || zc.AvailableBytesOut == 0);

                zc.EndDeflate();
                return(ms.ToArray());
            }
        }
        private void _FlushFinish()
        {
            // After writing a series of compressed buffers, each one closed
            // with Flush.Sync, we now write the final one as Flush.Finish,
            // and then stop.
            byte[] buffer     = new byte[128];
            var    compressor = new ZlibCodec();
            int    rc         = compressor.InitializeDeflate(_compressLevel, false);

            compressor.InputBuffer       = null;
            compressor.NextIn            = 0;
            compressor.AvailableBytesIn  = 0;
            compressor.OutputBuffer      = buffer;
            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)
            {
                TraceOutput(TraceBits.EmitBegin,
                            "Emit     begin    flush bytes({0})",
                            buffer.Length - compressor.AvailableBytesOut);

                _outStream.Write(buffer, 0, buffer.Length - compressor.AvailableBytesOut);

                TraceOutput(TraceBits.EmitDone,
                            "Emit     done     flush");
            }

            compressor.EndDeflate();

            _Crc32 = _runningCrc.Crc32Result;
        }
示例#8
0
文件: ZlibWriter.cs 项目: lulzzz/Port
        private async ValueTask RunZlibCompress()
        {
            var zlibCodec = new ZlibCodec();
            var buffer    = new byte[1024];

            zlibCodec.OutputBuffer = buffer;

            var result = zlibCodec.InitializeDeflate();

            if (result < 0)
            {
                throw new InvalidOperationException(
                          $"Got error code {result} when initializing deflate routine: {zlibCodec.Message}");
            }
            result = zlibCodec.SetDictionary(_dictionary);
            if (result < 0)
            {
                throw new InvalidOperationException(
                          $"Got error code {result} when setting dictionary: {zlibCodec.Message}");
            }

            Exception?exception = null;

            try
            {
                System.IO.Pipelines.ReadResult inputBuffer;
                do
                {
                    inputBuffer = await _pipe
                                  .Reader.ReadAsync(CancellationToken)
                                  .ConfigureAwait(false);

                    foreach (var input in inputBuffer.Buffer)
                    {
                        zlibCodec.NextIn           = 0;
                        zlibCodec.InputBuffer      = input.ToArray();
                        zlibCodec.AvailableBytesIn = input.Length;

                        while (zlibCodec.AvailableBytesIn > 0)
                        {
                            zlibCodec.NextOut           = 0;
                            zlibCodec.AvailableBytesOut = buffer.Length;

                            var start = zlibCodec.NextOut;
                            result = zlibCodec.Deflate(FlushType.None);
                            if (result < 0)
                            {
                                throw new InvalidOperationException(
                                          $"Got error code {result} when deflating the stream: {zlibCodec.Message}");
                            }

                            var length = zlibCodec.NextOut - start;

                            await _outputStream.WriteAsync(
                                buffer, start, length,
                                CancellationToken)
                            .ConfigureAwait(false);
                        }
                    }
                    _pipe.Reader.AdvanceTo(inputBuffer.Buffer.End);
                } while (inputBuffer.HasMoreData());

                zlibCodec.InputBuffer       = Array.Empty <byte>();
                zlibCodec.NextIn            = 0;
                zlibCodec.AvailableBytesIn  = 0;
                zlibCodec.NextOut           = 0;
                zlibCodec.AvailableBytesOut = buffer.Length;
                var bufferStart = zlibCodec.NextOut;

                result = zlibCodec.Deflate(FlushType.Sync);
                if (result != ZlibConstants.Z_OK)
                {
                    throw new InvalidOperationException($"Expected OK, got {result}");
                }
                result = zlibCodec.Deflate(FlushType.Finish);
                if (result != ZlibConstants.Z_STREAM_END)
                {
                    throw new InvalidOperationException($"Expected END, got {result}");
                }
                var bufferLength = zlibCodec.NextOut - bufferStart;

                await _outputStream.WriteAsync(
                    buffer, bufferStart, bufferLength,
                    CancellationToken)
                .ConfigureAwait(false);

                await _outputStream.FlushAsync(CancellationToken)
                .ConfigureAwait(false);
            }
            catch (Exception ex)
            {
                exception = ex;
            }
            finally
            {
                zlibCodec.EndDeflate();
                await _pipe.Reader.CompleteAsync(exception)
                .ConfigureAwait(false);
            }
        }
        public void WriteSkimToFile(double[][] skim, string filename, int[] mapping, int count, int transport, int startTime,
                                    int endTime, int factor)
        {
            var file = new FileInfo(filename);

            using (var writer = new BinaryWriter(file.Open(FileMode.Create, FileAccess.Write, FileShare.ReadWrite)))
            {
                //var position = 0L;
                double colSum      = GetColumnSums(skim);
                double diagonalSum = GetDiagonalSum(skim);

                string header = "\nMuuli-Matrix im gepackten Binary format.\nBezirke: " +
                                count + " \nVarTyp: 5 \nGesamtsumme: " +
                                colSum + " \nDiagonalsumme: " +
                                diagonalSum + " \nVMittel: " + transport +
                                " \nvon: " + startTime + " \nbis: " +
                                endTime + " \nFaktor: " + factor + " \n";
                short headerlength = (short)header.Length;

                int dataType  = 5;
                int roundProc = 1;

                //write header info
                writer.Write((short)3);
                writer.Write("$BI".ToCharArray());
                writer.Write((short)headerlength);
                writer.Write(header.ToCharArray());

                //write additional header info
                writer.Write((Int32)transport);
                writer.Write((float)startTime);
                writer.Write((float)endTime);
                writer.Write((float)factor);
                writer.Write((Int32)count);
                writer.Write((short)dataType);

                writer.Write(roundProc == 1 ? '\x01' : '\x00');

                for (int x = 0; x < count; x++)
                {
                    writer.Write((Int32)mapping[x]);
                }

                for (int x = 0; x < count; x++)
                {
                    writer.Write((Int32)mapping[x]);
                }

                writer.Write('\x00');
                writer.Write(diagonalSum);

                for (int i = 0; i < count; i++)
                {
                    MemoryStream toCompressFileStream = new MemoryStream();
                    BinaryWriter compressedWriter     = new BinaryWriter(toCompressFileStream);
                    for (int j = 0; j < count; j++)
                    {
                        compressedWriter.Write(skim[i][j]);
                    }
                    compressedWriter.Flush();

                    int    outputSize = sizeof(double) * count;
                    byte[] output     = new Byte[outputSize];
                    using (MemoryStream ms = new MemoryStream())
                    {
                        ZlibCodec compressor = new ZlibCodec();
                        compressor.InitializeDeflate(Ionic.Zlib.CompressionLevel.None, true);

                        compressor.InputBuffer      = toCompressFileStream.ToArray();
                        compressor.AvailableBytesIn = (int)toCompressFileStream.Length;
                        compressor.NextIn           = 0;
                        compressor.OutputBuffer     = output;

                        foreach (var f in new FlushType[] { FlushType.None, FlushType.Finish })
                        {
                            int bytesToWrite = 0;
                            do
                            {
                                compressor.AvailableBytesOut = outputSize;
                                compressor.NextOut           = 0;
                                compressor.Deflate(f);

                                bytesToWrite = outputSize - compressor.AvailableBytesOut;
                                if (bytesToWrite > 0)
                                {
                                    ms.Write(output, 0, bytesToWrite);
                                }
                            } while ((f == FlushType.None && (compressor.AvailableBytesIn != 0 || compressor.AvailableBytesOut == 0)) ||
                                     (f == FlushType.Finish && bytesToWrite != 0));
                        }

                        compressor.EndDeflate();

                        ms.Flush();
                        writer.Write((Int32)ms.Length);
                        writer.Write(ms.ToArray());
                        writer.Write(GetRowSum(skim, i));
                        writer.Write(GetColumnSum(skim, i));
                    }
                }
            }
        }
    private void Run()
    {
        int rc;
        int bufferSize = 40000;

        byte[] compressedBytes   = new byte[bufferSize];
        byte[] decompressedBytes = new byte[bufferSize];

        ZlibCodec compressingStream = new ZlibCodec();

        rc = compressingStream.InitializeDeflate(CompressionLevel.LEVEL9_BEST_COMPRESSION);
        CheckForError(compressingStream, rc, "InitializeDeflate");

        string dictionaryWord = "hello ";

        byte[] dictionary     = System.Text.ASCIIEncoding.ASCII.GetBytes(dictionaryWord);
        string TextToCompress = "hello, hello!  How are you, Joe? ";

        byte[] BytesToCompress = System.Text.ASCIIEncoding.ASCII.GetBytes(TextToCompress);

        rc = compressingStream.SetDictionary(dictionary);
        CheckForError(compressingStream, rc, "SetDeflateDictionary");

        long dictId = compressingStream.Adler32;

        compressingStream.OutputBuffer      = compressedBytes;
        compressingStream.NextOut           = 0;
        compressingStream.AvailableBytesOut = bufferSize;

        compressingStream.InputBuffer      = BytesToCompress;
        compressingStream.NextIn           = 0;
        compressingStream.AvailableBytesIn = BytesToCompress.Length;

        rc = compressingStream.Deflate(ZlibConstants.Z_FINISH);
        if (rc != ZlibConstants.Z_STREAM_END)
        {
            System.Console.Out.WriteLine("deflate should report Z_STREAM_END");
            System.Environment.Exit(1);
        }
        rc = compressingStream.EndDeflate();
        CheckForError(compressingStream, rc, "deflateEnd");

        ZlibCodec decompressingStream = new ZlibCodec();

        decompressingStream.InputBuffer      = compressedBytes;
        decompressingStream.NextIn           = 0;
        decompressingStream.AvailableBytesIn = bufferSize;

        rc = decompressingStream.InitializeInflate();
        CheckForError(decompressingStream, rc, "inflateInit");
        decompressingStream.OutputBuffer      = decompressedBytes;
        decompressingStream.NextOut           = 0;
        decompressingStream.AvailableBytesOut = decompressedBytes.Length;

        while (true)
        {
            rc = decompressingStream.Inflate(ZlibConstants.Z_NO_FLUSH);
            if (rc == ZlibConstants.Z_STREAM_END)
            {
                break;
            }
            if (rc == ZlibConstants.Z_NEED_DICT)
            {
                if ((int)decompressingStream.Adler32 != (int)dictId)
                {
                    System.Console.Out.WriteLine("unexpected dictionary");
                    System.Environment.Exit(1);
                }
                rc = decompressingStream.SetDictionary(dictionary);
            }
            CheckForError(decompressingStream, rc, "inflate with dict");
        }

        rc = decompressingStream.EndInflate();
        CheckForError(decompressingStream, rc, "EndInflate");

        int j = 0;

        for (; j < decompressedBytes.Length; j++)
        {
            if (decompressedBytes[j] == 0)
            {
                break;
            }
        }

        var result = System.Text.ASCIIEncoding.ASCII.GetString(decompressedBytes, 0, j);

        Console.WriteLine("orig length: {0}", TextToCompress.Length);
        Console.WriteLine("compressed length: {0}", compressingStream.TotalBytesOut);
        Console.WriteLine("decompressed length: {0}", decompressingStream.TotalBytesOut);
        Console.WriteLine("result length: {0}", result.Length);
        Console.WriteLine("result of inflate:\n{0}", result);
    }
示例#11
0
    private void Run()
    {
        int rc;
        int comprLen   = 40000;
        int uncomprLen = comprLen;

        byte[] CompressedBytes   = new byte[comprLen];
        byte[] DecompressedBytes = new byte[uncomprLen];
        string TextToCompress    = "This is the text that will be compressed.";

        byte[] BytesToCompress = System.Text.ASCIIEncoding.ASCII.GetBytes(TextToCompress);

        ZlibCodec compressor = new ZlibCodec(CompressionMode.Compress);

        compressor.InputBuffer       = BytesToCompress;
        compressor.NextIn            = 0;
        compressor.OutputBuffer      = CompressedBytes;
        compressor.NextOut           = 0;
        compressor.AvailableBytesIn  = 3;
        compressor.AvailableBytesOut = CompressedBytes.Length;

        rc = compressor.Deflate(ZlibConstants.Z_FULL_FLUSH);
        CheckForError(compressor, rc, "Deflate");

        CompressedBytes[3]++; // force an error in first compressed block // dinoch
        compressor.AvailableBytesIn = TextToCompress.Length - 3;

        rc = compressor.Deflate(ZlibConstants.Z_FINISH);
        if (rc != ZlibConstants.Z_STREAM_END)
        {
            CheckForError(compressor, rc, "Deflate");
        }
        rc = compressor.EndDeflate();
        CheckForError(compressor, rc, "EndDeflate");
        comprLen = (int)(compressor.TotalBytesOut);

        ZlibCodec decompressor = new ZlibCodec(CompressionMode.Decompress);

        decompressor.InputBuffer      = CompressedBytes;
        decompressor.NextIn           = 0;
        decompressor.AvailableBytesIn = 2;

        decompressor.OutputBuffer      = DecompressedBytes;
        decompressor.NextOut           = 0;
        decompressor.AvailableBytesOut = DecompressedBytes.Length;

        rc = decompressor.Inflate(ZlibConstants.Z_NO_FLUSH);
        CheckForError(decompressor, rc, "Inflate");

        decompressor.AvailableBytesIn = CompressedBytes.Length - 2;

        rc = decompressor.SyncInflate();
        CheckForError(decompressor, rc, "SyncInflate");

        bool gotException = false;

        try
        {
            rc = decompressor.Inflate(ZlibConstants.Z_FINISH);
        }
        catch (ZlibException ex1)
        {
            Console.WriteLine("Got Expected Exception: " + ex1);
            gotException = true;
        }

        if (!gotException)
        {
            System.Console.Out.WriteLine("inflate should report DATA_ERROR");
            /* Because of incorrect adler32 */
            System.Environment.Exit(1);
        }

        rc = decompressor.EndInflate();
        CheckForError(decompressor, rc, "EndInflate");

        int j = 0;

        for (; j < DecompressedBytes.Length; j++)
        {
            if (DecompressedBytes[j] == 0)
            {
                break;
            }
        }

        var result = System.Text.ASCIIEncoding.ASCII.GetString(DecompressedBytes, 0, j);

        Console.WriteLine("orig length: {0}", TextToCompress.Length);
        Console.WriteLine("compressed length: {0}", compressor.TotalBytesOut);
        Console.WriteLine("uncompressed length: {0}", decompressor.TotalBytesOut);
        Console.WriteLine("result length: {0}", result.Length);
        Console.WriteLine("result of inflate:\n(Thi){0}", result);
    }
    private void Run()
    {
        int rc;
        int j;
        int bufferSize = 40000;

        byte[] compressedBytes   = new byte[bufferSize];
        byte[] bufferToCompress  = new byte[bufferSize];
        byte[] decompressedBytes = new byte[bufferSize];

        ZlibCodec compressingStream = new ZlibCodec();

        rc = compressingStream.InitializeDeflate(CompressionLevel.BestSpeed);
        CheckForError(compressingStream, rc, "InitializeDeflate");

        compressingStream.OutputBuffer      = compressedBytes;
        compressingStream.NextOut           = 0;
        compressingStream.AvailableBytesOut = compressedBytes.Length;

        // At this point, bufferToCompress is all zeroes, so it should compress
        // very well:
        compressingStream.InputBuffer      = bufferToCompress;
        compressingStream.AvailableBytesIn = bufferToCompress.Length;
        rc = compressingStream.Deflate(FlushType.None);
        CheckForError(compressingStream, rc, "deflate");
        if (compressingStream.AvailableBytesIn != 0)
        {
            System.Console.Out.WriteLine("deflate not greedy");
            System.Environment.Exit(1);
        }

        Console.WriteLine("Stage 1: uncompressed bytes in so far:  {0,6}", compressingStream.TotalBytesIn);
        Console.WriteLine("          compressed bytes out so far:  {0,6}", compressingStream.TotalBytesOut);


        // Feed in already compressed data and switch to no compression:
        compressingStream.SetDeflateParams(CompressionLevel.None, CompressionStrategy.Default);
        compressingStream.InputBuffer      = compressedBytes;
        compressingStream.NextIn           = 0;
        compressingStream.AvailableBytesIn = bufferSize / 2; // why? - for fun
        rc = compressingStream.Deflate(FlushType.None);
        CheckForError(compressingStream, rc, "Deflate");

        Console.WriteLine("Stage 2: uncompressed bytes in so far:  {0,6}", compressingStream.TotalBytesIn);
        Console.WriteLine("          compressed bytes out so far:  {0,6}", compressingStream.TotalBytesOut);

        // Insert data into bufferToCompress, and Switch back to compressing mode:
        System.Random rnd = new Random();

        for (int i = 0; i < bufferToCompress.Length / 1000; i++)
        {
            byte b = (byte)rnd.Next();
            int  n = 500 + rnd.Next(500);
            for (j = 0; j < n; j++)
            {
                bufferToCompress[j + i] = b;
            }
            i += j - 1;
        }

        compressingStream.SetDeflateParams(CompressionLevel.BestCompression, CompressionStrategy.Filtered);
        compressingStream.InputBuffer      = bufferToCompress;
        compressingStream.NextIn           = 0;
        compressingStream.AvailableBytesIn = bufferToCompress.Length;
        rc = compressingStream.Deflate(FlushType.None);
        CheckForError(compressingStream, rc, "Deflate");

        Console.WriteLine("Stage 3: uncompressed bytes in so far:  {0,6}", compressingStream.TotalBytesIn);
        Console.WriteLine("          compressed bytes out so far:  {0,6}", compressingStream.TotalBytesOut);

        rc = compressingStream.Deflate(FlushType.Finish);
        if (rc != ZlibConstants.Z_STREAM_END)
        {
            Console.WriteLine("deflate reported {0}, should report Z_STREAM_END", rc);
            Environment.Exit(1);
        }
        rc = compressingStream.EndDeflate();
        CheckForError(compressingStream, rc, "EndDeflate");

        Console.WriteLine("Stage 4: uncompressed bytes in (final): {0,6}", compressingStream.TotalBytesIn);
        Console.WriteLine("          compressed bytes out (final): {0,6}", compressingStream.TotalBytesOut);

        ZlibCodec decompressingStream = new ZlibCodec(CompressionMode.Decompress);

        decompressingStream.InputBuffer      = compressedBytes;
        decompressingStream.NextIn           = 0;
        decompressingStream.AvailableBytesIn = bufferSize;

        // upon inflating, we overwrite the decompressedBytes buffer repeatedly
        while (true)
        {
            decompressingStream.OutputBuffer      = decompressedBytes;
            decompressingStream.NextOut           = 0;
            decompressingStream.AvailableBytesOut = decompressedBytes.Length;
            rc = decompressingStream.Inflate(FlushType.None);
            if (rc == ZlibConstants.Z_STREAM_END)
            {
                break;
            }
            CheckForError(decompressingStream, rc, "inflate large");
        }

        rc = decompressingStream.EndInflate();
        CheckForError(decompressingStream, rc, "EndInflate");

        if (decompressingStream.TotalBytesOut != 2 * decompressedBytes.Length + bufferSize / 2)
        {
            System.Console.WriteLine("bad large inflate: " + decompressingStream.TotalBytesOut);
            System.Environment.Exit(1);
        }

        for (j = 0; j < decompressedBytes.Length; j++)
        {
            if (decompressedBytes[j] == 0)
            {
                break;
            }
        }

        Console.WriteLine("compressed length: {0}", compressingStream.TotalBytesOut);
        Console.WriteLine("decompressed length (expected): {0}", 2 * decompressedBytes.Length + bufferSize / 2);
        Console.WriteLine("decompressed length (actual)  : {0}", decompressingStream.TotalBytesOut);
    }