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
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();
        }
        /// <summary>Compresses the specified byte range using the
        /// specified compressionLevel (constants are defined in
        /// java.util.zip.Deflater). 
        /// </summary>
        public static byte[] Compress(byte[] value_Renamed, int offset, int length, int compressionLevel)
        {
            /* 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. */
            System.IO.MemoryStream bos = new System.IO.MemoryStream(length);

            Deflater compressor = new Deflater();

            try
            {
                compressor.SetLevel(compressionLevel);
                compressor.SetInput(value_Renamed, offset, length);
                compressor.Finish();

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

            return bos.ToArray();
        }
Example #3
0
		public static byte[] Compress(byte[] content)
		{
			//return content;
			Deflater compressor = new Deflater();
			compressor.SetLevel(Deflater.BEST_COMPRESSION);

			compressor.SetInput(content);
			compressor.Finish();

			using (MemoryStream bos = new MemoryStream(content.Length))
			{
				var buf = new byte[1024];
				while (!compressor.IsFinished)
				{
					int n = compressor.Deflate(buf);
					bos.Write(buf, 0, n);
				}
				return bos.ToArray();
			}
		}
Example #4
0
 public static byte[] ZipBytes(byte[] input)
 {
     MemoryStream ms = new MemoryStream();
     Deflater zipper = new Deflater();
     zipper.SetLevel(5);
     Stream st = new DeflaterOutputStream(ms, zipper);
     st.Write(input, 0, input.Length);
     st.Flush();
     st.Close();
     byte[] result = (byte[])ms.ToArray();
     return result;
 }