Skip to content
Related Articles

Related Articles

Improve Article
Deflater deflate() function in Java with examples
  • Last Updated : 24 Jun, 2019


The deflate() function of the Deflater class in java.util.zip is used to compress the input data and fill the given buffer with the compressed data. The function returns the number of bytes of the compressed data.

Function Signature:

public int deflate(byte[] b)
public int deflate(byte[] b, int offset, int length, int flush)
public int deflate(byte[] b, int offset, int length)

Syntax:

d.deflate(byte[])
d.deflate(byte[], int, int, int)
d.deflate(byte[], int, int )

Parameter: The various parameters accepted by these overloaded functions are:

  • byte[] b: This is the input array that is to be deflated
  • int offset: This is the starting offset from which the values are to be read in the given array
  • int length: This is the maximum length to be compressed from the starting offset.
  • int flush: This is the flush mode passed as the parameter

Return Type: The function returns an integer value which is the size of the compressed data.



Exception: The function throws IllegalArgumentException if is the flush mode is invalid. There are three valid flush modes which are NO_FLUSH, SYNC_FLUSH, FULL_FLUSH.

Below examples demonstrate the use of the above function:

Example 1: To demonstrate the use of deflate(byte[] b) function




// Java program to demonstrate
// the use of deflate(byte[] b) function
  
import java.util.zip.*;
import java.io.UnsupportedEncodingException;
  
class GFG {
    public static void main(String args[])
        throws UnsupportedEncodingException
    {
        // deflater
        Deflater d = new Deflater();
  
        // get the text
        String pattern = "GeeksforGeeks", text = "";
  
        // generate the text
        for (int i = 0; i < 4; i++)
            text += pattern;
  
        // 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 :"
                           + new String(output)
                           + "\n Size " + size);
  
        // original String
        System.out.println("Original String :"
                           + text + "\n Size "
                           + text.length());
  
        // end
        d.end();
    }
}

Output:

Compressed String :x?sOM?.N?/r???q??
 Size 21
Original String :GeeksforGeeksGeeksforGeeksGeeksforGeeksGeeksforGeeks
 Size 52

Example 2: To demonstrate the use of deflate(byte[] b, int offset, int length) function




// Java program to demonstrate the use
// of deflate(byte[] b, int offset, int length) function
  
import java.util.zip.*;
import java.io.UnsupportedEncodingException;
  
class GFG {
    public static void main(String args[])
        throws UnsupportedEncodingException
    {
        // deflater
        Deflater d = new Deflater();
  
        // get the text
        String pattern = "GeeksforGeeks", text = "";
  
        // generate the text
        for (int i = 0; i < 4; i++)
            text += pattern;
  
        // set the input for deflator
        d.setInput(text.getBytes("UTF-8"));
  
        // finish
        d.finish();
  
        // output bytes
        byte output[] = new byte[1024];
  
        // compress the data, with given offset and
        // set maximum size of compressed string
        int size = d.deflate(output, 2, 13);
  
        // compressed String
        System.out.println("Compressed String :"
                           + new String(output)
                           + "\n Size " + size);
  
        // original String
        System.out.println("Original String :"
                           + text + "\n Size "
                           + text.length());
  
        // end
        d.end();
    }
}

Output:

Compressed String :x?sOM?.N?/r?
 Size 13
Original String :GeeksforGeeksGeeksforGeeksGeeksforGeeksGeeksforGeeks
 Size 52

Example 3: To demonstrate the use of deflate(byte[] b, int offset, int length, int flush) function




// Java program to demonstrate the use of
// deflate(byte[] b, int offset, int length, int flush) function
  
import java.util.zip.*;
import java.io.UnsupportedEncodingException;
  
class GFG {
    public static void main(String args[])
        throws UnsupportedEncodingException
    {
        // deflater
        Deflater d = new Deflater();
  
        // get the text
        String pattern = "GeeksforGeeks", text = "";
  
        // generate the text
        for (int i = 0; i < 4; i++)
            text += pattern;
  
        // set the input for deflator
        d.setInput(text.getBytes("UTF-8"));
  
        // finish
        d.finish();
  
        // output bytes
        byte output[] = new byte[1024];
  
        // compress the data, with given offset and
        // set maximum size of compressed string
        // and specified Flush
        int size = d.deflate(output, 2, 13, Deflater.FULL_FLUSH);
  
        // compressed String
        System.out.println("Compressed String :"
                           + new String(output)
                           + "\n Size " + size);
  
        // original String
        System.out.println("Original String :" + text
                           + "\n Size " + text.length());
  
        // end
        d.end();
    }
}

Output:

Compressed String :x?sOM?.N?/r?
 Size 13
Original String :GeeksforGeeksGeeksforGeeksGeeksforGeeksGeeksforGeeks
 Size 52

Reference:

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :