ICSharpCode.SharpZipLib.Zip.Compression.DeflaterEngine.SetLevel C# (CSharp) Method

SetLevel() public method

Set the deflate level (0-9)
public SetLevel ( int level ) : void
level int The value to set the level to.
return void
        public void SetLevel(int level)
        {
            if ((level < 0) || (level > 9)) {
                throw new ArgumentOutOfRangeException(nameof(level));
            }

            goodLength = DeflaterConstants.GOOD_LENGTH[level];
            max_lazy = DeflaterConstants.MAX_LAZY[level];
            niceLength = DeflaterConstants.NICE_LENGTH[level];
            max_chain = DeflaterConstants.MAX_CHAIN[level];

            if (DeflaterConstants.COMPR_FUNC[level] != compressionFunction) {

            #if DebugDeflation
                if (DeflaterConstants.DEBUGGING) {
                   Console.WriteLine("Change from " + compressionFunction + " to "
                                          + DeflaterConstants.COMPR_FUNC[level]);
                }
            #endif
                switch (compressionFunction) {
                    case DeflaterConstants.DEFLATE_STORED:
                        if (strstart > blockStart) {
                            huffman.FlushStoredBlock(window, blockStart,
                                strstart - blockStart, false);
                            blockStart = strstart;
                        }
                        UpdateHash();
                        break;

                    case DeflaterConstants.DEFLATE_FAST:
                        if (strstart > blockStart) {
                            huffman.FlushBlock(window, blockStart, strstart - blockStart,
                                false);
                            blockStart = strstart;
                        }
                        break;

                    case DeflaterConstants.DEFLATE_SLOW:
                        if (prevAvailable) {
                            huffman.TallyLit(window[strstart - 1] & 0xff);
                        }
                        if (strstart > blockStart) {
                            huffman.FlushBlock(window, blockStart, strstart - blockStart, false);
                            blockStart = strstart;
                        }
                        prevAvailable = false;
                        matchLen = DeflaterConstants.MIN_MATCH - 1;
                        break;
                }
                compressionFunction = DeflaterConstants.COMPR_FUNC[level];
            }
        }

Usage Example

Example #1
0
        /// <summary>
        /// Sets the compression level.  There is no guarantee of the exact
        /// position of the change, but if you call this when needsInput is
        /// true the change of compression level will occur somewhere near
        /// before the end of the so far given input.
        /// </summary>
        /// <param name="lvl">
        /// the new compression level.
        /// </param>
        public void SetLevel(int lvl)
        {
            if (lvl == DEFAULT_COMPRESSION)
            {
                lvl = 6;
            }
            else if (lvl < NO_COMPRESSION || lvl > BEST_COMPRESSION)
            {
                throw new ArgumentOutOfRangeException("lvl");
            }

            if (level != lvl)
            {
                level = lvl;
                engine.SetLevel(lvl);
            }
        }