How to Compress file using Deflater class in java


 

How to Compress file using Deflater class in java

In this tutorial you will see the use of Deflater class for compressing the data of a file.

In this tutorial you will see the use of Deflater class for compressing the data of a file.

How to Compress file using Deflater class in java.

In this Example, we will discuss  how to use Deflater class in java. The Deflater class is used to compressed data by using ZLIB library. The Deflater class is available in java.util.zip package. The finish() method of Deflater class is used to indicate that compression of data is finished. 
     Deflater is use Dictionary-Based Compression .This tries to replace repeated patterns of the data with the reference of the first occurrence of repeated pattern.

In this Example, we will compressed the data of a file. The Defleter class create a deflater object for compressing data.  The FileInputStream class creates a input stream for reading data from a file. The read() method of  FileInputStream class read data from input stream. The finish() method of Deflater class is used to indicate that compression of data is finished. The finished() method of Deflater class returns true if output stream reached at the end of compressed data. The setInput() method of Deflater class is used to input data for compression. 

About Deflater API:

Return Type Method Description 
int deflater(byte[] b) The deflater() method store compressed data into  associated buffer.
void finished() The finished() method  returns true if output stream reached at the of compressed data
void setInput() The setInput() method set data for compression.
void finish() Thefinish() method define that compression will be finished .

Code

import java.io.*;
import java.util.zip.*;
import java.util.zip.Deflater;
import java.io.FileOutputStream;
import java.io.FileInputStream;

public class DeflaterDemo2 {
  public final static String SUFFIX = ".txt";

  public static void main(String[] argsthrows IOException {

    Deflater defl1 = new Deflater();
    byte[] inBuffer = new byte[1024];
    byte[] outBuffer = new byte[1024];
    int compBytes;
    for (int i = 0; i < args.length; i++) {
      FileInputStream finStream = new FileInputStream(args[i]);
      FileOutputStream foutStream = new FileOutputStream(args[i+ SUFFIX);
      while (true) {
        int readNum = finStream.read(inBuffer);
        if (readNum == -1) {
          defl1.finish();
          while (!defl1.finished()) {
            compBytes = defl1.deflate(outBuffer, 0,
                outBuffer.length);
            if (compBytes > 0) {
              foutStream.write(outBuffer, 0, compBytes);
            }
          }
          break;
        else {
          defl1.setInput(inBuffer, 0, readNum);
          while (!defl1.needsInput()) {
            compBytes = defl1.deflate(outBuffer, 0,
                outBuffer.length);
            if (compBytes > 0) {
              foutStream.write(outBuffer, 0, compBytes);
            }
          }
        }
      }
      finStream.close();
      foutStream.flush();
      foutStream.close();
      defl1.reset();
    }
    System.out.println("All the file's data Successfully Compressed ");
  }
}

Following is the output if you run the application:

C:\>java DeflaterDemo2  C:\Work\Bharat\Temp\Deflater2\bharat.txt
All the file's data Successfully Compressed
C:\Work\Bharat\Temp\Deflater2>

Ads