Source Code : Compress string(byte array) by Deflater

Java Is Open Source Programming Language You Can Download From Java and Java Libraries From http://www.oracle.com. Click Here to download
We provide this code related to title for you to solve your developing problem easily. Libraries which is import in this program you can download from http://www.oracle.com. Click Here or search from google with Libraries Name you get jar file related it

Compress string(byte array) by Deflater

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

public class Main {
  public static void main(String[] args) throws IOException {
    Deflater def = new Deflater();
    byte[] input = new byte[1024];
    byte[] output = new byte[1024];

    FileInputStream fin = new FileInputStream("a.dat");
    FileOutputStream fout = new FileOutputStream("b.dat");

    int numRead = fin.read(input);

    def.setInput(input, 0, numRead);

    while (!def.needsInput()) {
      int numCompressedBytes = def.deflate(output, 0, output.length);
      if (numCompressedBytes > 0) {
        fout.write(output, 0, numCompressedBytes);
      }
    }
    def.finish();
    fin.close();
    fout.flush();
    fout.close();
    def.reset();
  }
}

   
    
    
    
    
    
    
    
    
  

Thank with us