Java I/O How to - Zip a file byte by byte








Question

We would like to know how to zip a file byte by byte.

Answer

 /*  w ww .j  av  a 2s .co m*/

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.zip.DeflaterOutputStream;

public class FileDeflater {
  public static void main(String[] args) throws Exception {
    FileInputStream fin = new FileInputStream("a.dat");
    FileOutputStream fout = new FileOutputStream("b.dat");
    DeflaterOutputStream dos = new DeflaterOutputStream(fout);
    for (int c = fin.read(); c != -1; c = fin.read()) {
      dos.write(c);
    }
    dos.close();
    fin.close();
  }
}

The code above generates the following result.