Open In App

Java.util.zip.InflaterOutputStream class in Java

Last Updated : 12 Sep, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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




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


Output :

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

Program 2:




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


Output :

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

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



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads