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

// Test deflate / inflate

class Mytool1
{
     
   public static void main( String args[])
   {      
      String fname;
      String bname;
      
      FileInputStream  fin;       
      FileOutputStream fout;      
      
      int len,size,i;
      byte b[];
      byte bout[]; 
      byte btxt[];

      if ( args.length > 0 )
      {
         fname = args[0];
         len = fname.length();
         bname = fname.substring( 0, len-2 ) + "64" ; 
         System.err.println( "filename " + fname);
         System.err.println( "filename " + bname);
               
      }
      else
      {
         System.err.println( "use java Mytool1 <filename>");
         return;
      }        
            
            // read binary Datenstrom                                         
      try
      {         
         fin = new FileInputStream( fname );                     
         b = new byte[ 1500000 ];               // input  Buffer
         bout = new byte[ 1500000 ];            // output Buffer                            
         
         try
         {
            len = fin.read( b );
            System.err.println( "read " + fname + ": "+ len + " bytes");
            
            Deflater compressor = new Deflater();
            compressor.setInput( b );
            compressor.finish( );
            len = compressor.deflate( bout );
            
            System.err.println( "compressed to "+ len + " bytes");
            btxt = codiere2base64( bout , len);            
            System.err.println( "base64 to "+ btxt.length + " bytes");  
            try
            { 
               fout = new FileOutputStream( bname ); 
               fout.write( btxt );
               fout.close();               
            }  
            catch (IOException e)
            {
               System.err.println( "file " + fname + " error:" + e );
            }                     
         }
         catch (IOException e)
         {
            System.err.println( "file " + fname + " error:" + e );
         }         
         fin.close();
      }
      catch (IOException e)
      {
         System.err.println( "file " + fname + " error:" + e );
      }     
   }   
   
   /***************************************************************
   **  codiere String to base64 
   */    
   static byte[] codiere2base64( byte in[] , int len )
   {
      byte b64[]= { 'A','B','C','D','E','F','G','H','I','J',
                    'K','L','M','N','O','P','Q','R','S','T',
                    'U','V','W','X','Y','Z','a','b','c','d',
                    'e','f','g','h','i','j','k','l','m','n',
                    'o','p','q','r','s','t','u','v','w','x',
                    'y','z','0','1','2','3','4','5','6','7',
                    '8','9','+','/'
                   }; 	
   	
      int i=0, rest, j, target, p=0;
      byte ca[];
      String s;       
      
      target = (len/3) * 4;
      rest = len % 3;
      if ( rest > 0 )
         target += 4;
      
      ca = new byte[ target ];   
             
      
      if ( len > 2 )
      {       
         for ( i = 0; i < len-2; i += 3 )
         {
            j = ((in[i] & 0xff) << 16) + ((in[i+1] & 0xff) << 8) + (in[i+2] & 0xff);
            ca[p++] = b64[(j >> 18) & 0x3f];
            ca[p++] = b64[(j >> 12) & 0x3f];
            ca[p++] = b64[(j >> 6) & 0x3f];
            ca[p++] = b64[j & 0x3f];
         }
      }
      rest = len - i;
      if ( rest == 2 )
      {    
         j = ((in[i] & 0xff) << 8) + (in[i+1] & 0xff);      //  16 Bit / 6  = 3 Byte
         ca[p++] = b64[(j >> 12) & 0x0F];
         ca[p++] = b64[(j >> 6) & 0x3f];
         ca[p++] = b64[(j ) & 0x3F];
         ca[p++] = '=' ;        
      }
      if ( rest == 1 )
      {   
         j = in[i] & 0xff;                                //  8 Bit / 6  = 2 Byte
         ca[p++] = b64[(j >> 6) & 0x03];
         ca[p++] = b64[(j ) & 0x3F]; 
         ca[p++] = '=' ;  
         ca[p++] = '=' ;           
      }
             
      return ca; 
   }
}