Deflater deflate() function in Java with examples


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

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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();
    }
}

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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();
    }
}

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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();
    }
}

chevron_right


Output:

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

Reference:



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.