Open In App

Java.util.zip.DeflaterOutputStream class in Java

Java.util.zip.DeflaterInputStream class in Java

This class implements an output stream filter for compressing data in the “deflate” compression format. It is also used as the basis for other types of compression filters, such as GZIPOutputStream.
Constructors and Description



Methods:




//Java program to demonstrate DeflaterOutputStream
  
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.DeflaterOutputStream;
  
class DeflaterOutputStreamDemo
{
    public static void main(String[] args) throws IOException 
    {
  
        FileOutputStream fos = new FileOutputStream("file2.txt");
  
        //Assign FileOutputStream to DeflaterOutputStream
        DeflaterOutputStream dos = new DeflaterOutputStream(fos);
  
        //write it into DeflaterOutputStream
        for (int i = 0; i <10 ; i++) 
        {
            dos.write(i);
        }
          
        //illustrating flush() method()
        dos.flush();
          
        //illustrating finish()
        //Finishes writing compressed data to the output stream
        // without closing the underlying stream
        dos.finish();
          
        //fos is not closed
        //writing some data on file
        fos.write('G');
      
        //Writes remaining compressed data to the output stream
        // closes the underlying stream.
        dos.close();
    }
}

Note: Output of the program will not be visible on online IDE as file2.txt can not be read here.




Article Tags :