Java.util.zip.InflaterOutputStream class in Java

This class implements an output stream filter for uncompressing data stored in the “deflate” compression format.
Constructors

  • InflaterOutputStream(OutputStream out) : Creates a new output stream with a default decompressor and buffer size.
  • InflaterOutputStream(OutputStream out, Inflater infl) : Creates a new output stream with the specified decompressor and a default buffer size.
  • InflaterOutputStream(OutputStream out, Inflater infl, int bufLen) : Creates a new output stream with the specified decompressor and buffer size.

Methods:

  • void close() : Writes any remaining uncompressed data to the output stream and closes the underlying output stream.
    Syntax :
    public void close()
               throws IOException
    Overrides:
    close in class FilterOutputStream
    Throws:
    IOException
  • void finish() : Finishes writing uncompressed data to the output stream without closing the underlying stream.
    Syntax :public void finish()
                throws IOException
    Throws:
    IOException
  • void flush() : Flushes this output stream, forcing any pending buffered output bytes to be written.
    Syntax :public void flush()
               throws IOException
    Overrides:
    flush in class FilterOutputStream
    Throws:
    IOException
  • void write(byte[] b, int off, int len) : Writes an array of bytes to the uncompressed output stream.
    Syntax :public void write(byte[] b,
             int off,
             int len)
               throws IOException
    Overrides:
    write in class FilterOutputStream
    Parameters:
    b - buffer containing compressed data to decompress and write to the output stream
    off - starting offset of the compressed data within b
    len - number of bytes to decompress from b
    Throws:
    IndexOutOfBoundsException 
    IOException
    NullPointerException 
    ZipException
  • void write(int b) : Writes a byte to the uncompressed output stream.
    Syntax :public void write(int b)
               throws IOException
    Parameters:
    b - a single byte of compressed data to decompress and write to the output stream
    Throws:
    IOException
    ZipException 
    

Program 1

filter_none

edit
close

play_arrow

link
brightness_4
code

//Java program to illustrate InflaterInputStream class
import java.io.*;
import java.util.Arrays;
import java.util.zip.DeflaterInputStream;
import java.util.zip.InflaterOutputStream;
  
class InflaterOutputStreamDemo 
{
    public static void main(String[] args) throws IOException 
    {
        byte b[] = {1, 2, 3, 4, 5, 6};
        ByteArrayInputStream bin = new ByteArrayInputStream(b);
        DeflaterInputStream din = new DeflaterInputStream(bin);
        byte c[] = new byte[10];
        din.read(c);
        din.close();
  
  
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        InflaterOutputStream ios = new InflaterOutputStream(bos);
        //illustrating write(byte b[],int off,int len)
        ios.write(c);
  
        //flushing
        ios.flush();
          
        //Finishes writing uncompressed data to the output stream
        // without closing the underlying stream.
        ios.finish();
        System.out.println(Arrays.toString(bos.toByteArray()));
        bos.close();
          
        //illustrating close()
        ios.close();
    }
}

chevron_right


Output :

[1, 2, 3, 4, 5, 6]

Program 2:



filter_none

edit
close

play_arrow

link
brightness_4
code

//Java program to illustrate InflaterInputStream class
import java.io.*;
import java.util.Arrays;
import java.util.zip.DeflaterInputStream;
import java.util.zip.InflaterOutputStream;
  
class InflaterOutputStreamDemo 
{
    public static void main(String[] args) throws IOException 
    {
        byte b[] = {1, 2, 3, 4, 5, 6};
        ByteArrayInputStream bin = new ByteArrayInputStream(b);
        DeflaterInputStream din = new DeflaterInputStream(bin);
        FileOutputStream fos=new FileOutputStream("file.txt");
        byte c[] = new byte[10];
        din.read(c);
        fos.write(c);
        din.close();
        fos.close();
  
        //reading the compressed data
        FileInputStream fis = new FileInputStream("file.txt");
  
        ByteArrayOutputStream bos1=new ByteArrayOutputStream();
        InflaterOutputStream ios = new InflaterOutputStream(bos1);
        int ch;
          
        //illustrating write() method
        while ( (ch=fis.read() ) != -1
        {
            ios.write(ch);
        }
        System.out.print(Arrays.toString(bos1.toByteArray()));
    }
  
}

chevron_right


Output :

[1, 2, 3, 4, 5, 6]

Next article: Java.util.zip.InflaterInputStream class in Java

This article is contributed by Nishant Sharma. 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up
Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.