GZipCat.java



import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.PrintWriter;
import java.io.File;
import java.io.FileInputStream;
import java.util.zip.GZIPInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

/** gzip形式のファイルの内容を表示する GZipCat */

public class GZipCat {

  /** 最初に呼び出される処理 */

     public static void main( String argv[] ) {

          if( argv.length == 1 ) {
              try {
                   File file = new File( argv[0] );
                   readFile( file );
              }
              catch( FileNotFoundException e ){
                     System.out.println( "file not found" );
              }
              catch( IOException e ){
                     System.out.println( "IO error..." );
              }
         }
         else {
               System.out.println( "Usage:java GZipCat gzipfile" );
         }
   }

  /** 与えられたファイルの内容を表示する */

     public static void readFile( File file )
                         throws FileNotFoundException,IOException {

            try {
                 FileInputStream fis = new FileInputStream( file );
                 GZIPInputStream gzis = new GZIPInputStream( fis );
                 InputStreamReader isr = new InputStreamReader( gzis );
                 BufferedReader br = new BufferedReader( isr );
                 PrintWriter pw = new PrintWriter( System.out, true );
                 String s;
                 while( ( s = br.readLine() ) != null ) {
                    pw.println( s );
                 }
            fis.close();
            }
            catch( FileNotFoundException e ){
                   throw e;
            }
            catch( IOException e ){
                   throw e;
            }
      }
}