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

SetLevel() public method

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.
public SetLevel ( int level ) : void
level int /// the new compression level. ///
return void
        public void SetLevel(int level)
        {
            if (level == DEFAULT_COMPRESSION) {
                level = 6;
            } else if (level < NO_COMPRESSION || level > BEST_COMPRESSION) {
                throw new ArgumentOutOfRangeException(nameof(level));
            }

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

Usage Example

Example #1
1
        public byte[] Compress(byte[] input)
        {
            // Create the compressor with highest level of compression
            Deflater compressor = new Deflater();
            compressor.SetLevel(Deflater.BEST_COMPRESSION);

            // Give the compressor the data to compress
            compressor.SetInput(input);
            compressor.Finish();

            /*
             * Create an expandable byte array to hold the compressed data.
             * You cannot use an array that's the same size as the orginal because
             * there is no guarantee that the compressed data will be smaller than
             * the uncompressed data.
             */
            MemoryStream bos = new MemoryStream(input.Length);

            // Compress the data
            byte[] buf = new byte[1024];
            while (!compressor.IsFinished)
            {
                int count = compressor.Deflate(buf);
                bos.Write(buf, 0, count);
            }

            // Get the compressed data
            return bos.ToArray();
        }
All Usage Examples Of ICSharpCode.SharpZipLib.Zip.Compression.Deflater::SetLevel