java .gz file reading and writing

compression and decompression of .gz file


  • import java.io.FileInputStream;
  • import java.io.FileOutputStream;
  • import java.io.InputStream;
  • import java.io.OutputStream;
  • import java.util.zip.GZIPInputStream;
  • import java.util.zip.GZIPOutputStream;

  • public class CompressUncompress {

  • public static void main(String[] args) throws Exception {

  •     //compressing a file to .gz file
  • FileInputStream fis = new FileInputStream("original.txt");
  • FileOutputStream fos = new FileOutputStream("compressed.gz");
  • GZIPOutputStream gos = new GZIPOutputStream(fos);
  • Create(fis, gos);

  • //Uncompressing a file to .gz file
  • FileInputStream fis2 = new FileInputStream("compressed.gz");
  • GZIPInputStream gis = new GZIPInputStream(fis2);
  • FileOutputStream fos2 = new FileOutputStream("uncompressed.txt");
  • Create(gis, fos2);

  • }

  • public static void Create(InputStream is, OutputStream os) throws Exception {
  • int oneByte;
  • while ((oneByte = is.read()) != -1) {
  • os.write(oneByte);
  • }
  • os.close();
  • is.close();
  • }

  • }

Comments

Popular posts from this blog

Today Walkin 14th-Sept

Spring Elasticsearch Operations

Hibernate Search - Elasticsearch with JSON manipulation