How to Compress a File(.txt) and Read From the Compressed File(.zip) in Java?

The following example demonstrates the use of Java Programming Language in compressing files into .zip formats and reading from a zipped file (getting it back). While running the program please provide the name of the file which is to be zipped as a runtime argument(0) and the new name of the zipped file at argument(1).

e.g.: Java WriteZip file.txt MyZip.zip

import java.io.*;
import java.util.zip.*;

class WriteZip{
	public static void main(String[] args) throws Exception{
		int schunk=8192;
		
		FileOutputStream fos=new FileOutputStream(args[1]);
		GZIPOutputStream z=new GZIPOutputStream(fos);
		
		byte[] buffer=new byte[schunk];
		
		FileInputStream fis=new FileInputStream(args[0]);
		
		int length;
		while((length=fis.read(buffer, 0, schunk))!=-1){
			z.write(buffer, 0, length);
		}
			fis.close();
			z.close();
			fos.close();
	
	}
}
class ReadZip{
public static void main(String[] args) throws Exception{
	BufferedReader br=new BufferedReader(new InputStreamReader(new GZIPInputStream(new FileInputStream(args[0]))));
	String s;
	while((s=br.readLine())!=null){
		System.out.println(s);
	}
}
}

OUTPUT:
Zip