Deflater setLevel() function in Java with examples


The setLevel() function of the Deflater class in java.util.zip sets the current compression level to the specified value. The compression level is an integer value from 0 to 9.
Function Signature:

public void setLevel(int level)

Syntax:

d.setLevel(int);

Parameter: The function requires an integer value which is the specified compression value



Return Type: The function does not return any value.

Exception: The function throws IllegalArgumentException if the compression level is invalid.

Some constant values for deflater levels :

  • BEST_COMPRESSION : compression level for best compression
  • BEST_SPEED : Compression level for fastest compression.
  • DEFAULT_COMPRESSION : Default compression level.
  • NO_COMPRESSION : Compression level for no compression.
  • Example 1:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java program to describe the use
    // of setLevel() function
      
    import java.util.zip.*;
    import java.io.UnsupportedEncodingException;
      
    class GFG {
      
        // Function to compress the string to the given level
        static void compression(int level, String text)
            throws UnsupportedEncodingException
        {
            // deflater
            Deflater d = new Deflater(level);
      
            // set the Input for deflator
            d.setInput(text.getBytes("UTF-8"));
      
            // finish
            d.finish();
      
            // output bytes
            byte output[] = new byte[1024];
      
            // compress the data
            int size = d.deflate(output);
      
            // compressed String
            System.out.println("Compressed String with level ="
                               + level + " :"
                               + new String(output)
                               + "\n Size " + size);
      
            d.end();
        }
      
        // Driver code
        public static void main(String args[])
            throws UnsupportedEncodingException
        {
      
            // get the text
            String pattern = "GeeksforGeeks", text = "";
      
            // generate the text
            for (int i = 0; i < 4; i++)
                text += pattern;
      
            // original String
            System.out.println("Original String :" + text
                               + "\n Size " + text.length());
      
            // default
            compression(Deflater.DEFAULT_COMPRESSION, text);
      
            // no compression
            compression(Deflater.NO_COMPRESSION, text);
      
            // Best compression
            compression(Deflater.BEST_COMPRESSION, text);
      
            // Best Speed
            compression(Deflater.BEST_SPEED, text);
        }
    }

    chevron_right

    
    

    Output:

    Original String :GeeksforGeeksGeeksforGeeksGeeksforGeeksGeeksforGeeks
     Size 52
    Compressed String with level =-1 :x?sOM?.N?/r???q??
     Size 21
    Compressed String with level =0 :x4??GeeksforGeeksGeeksforGeeksGeeksforGeeksGeeksforGeeks??
     Size 63
    Compressed String with level =9 :x?sOM?.N?/r???q??
     Size 21
    Compressed String with level =1 :xsOM?.N?/r?`?0??
     Size 22
    

    Reference: https://docs.oracle.com/javase/7/docs/api/java/util/zip/Deflater.html#setLevel()



    My Personal Notes arrow_drop_up

    Second year Department of Information Technology Jadavpur University

    If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

    Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.