KickJava   Java API By Example, From Geeks To Geeks.

Java > Java SE, EE, ME > java > util > zip > GZIPInputStream

java.util.zip
Class GZIPInputStream

java.lang.Object
  extended by java.io.InputStream
      extended by java.io.FilterInputStream
          extended by java.util.zip.InflaterInputStream
              extended by java.util.zip.GZIPInputStream
All Implemented Interfaces:
Closeable
See Also:
Top Examples, Source Code

public void close()
           throws IOException
See Also:
FilterInputStream.in, InflaterInputStream, Closeable
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


protected CRC32 crc
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


protected boolean eos
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public GZIPInputStream(InputStream in)
                throws IOException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[172]Compress and decompress the data using GZIP
By Manivannan (manivana { at } yahoo { dot } com) on 2005/03/31 08:15:30  Rate
import java.util.zip.GZIPOutputStream; 
  import java.util.zip.GZIPInputStream; 
  import java.io.IOException; 
  import java.io.InputStream; 
  import java.io.OutputStream; 
  import java.io.BufferedReader; 
  import java.io.InputStreamReader; 
 /** 
  * The  < b >  < code > GZIPHandler < /code >  < /b >  compress and decompress the data using  
  * GZIPInputStream & GZIPOutputStream. 
  *  < br >  
  * 
  * @author Manivannan 
  */
 
  public class GZIPHandler  
   {  
   /** 
    * It compress the input String and write it to the OutputStream 
    * @param osStream - instance of OutputStream 
    * @param respMessage - uncompressesed response XML 
    */
 
   public void compress ( OutputStream outStream, String respMessage )  
              
    {  
     GZIPOutputStream gzos = null; 
     try 
      {  
       byte bytesData [  ]  = respMessage.getBytes (  ) ; 
       gzos = new GZIPOutputStream ( outStream, bytesData.length ) ; 
       gzos.write ( bytesData, 0, bytesData.length ) ; 
       gzos.finish (  ) ; 
      }  
     catch  ( IOException ioException )  
      {  
       System.out.println ( "Exception Occured in Catch Block"+ioExc.getMessage (  )  ) ; 
      }  
     finally 
      {  
       try 
        {  
         gzos.close (  ) ; 
        }  
       catch  ( IOException ioException )  
        {  
         System.out.println ( "Exception Occured in finally block"+ioExc.getMessage (  )  ) ; 
        }  
      }  
    }  
  
  
   /** 
    * It decompresses the compressed request XML String  
    * @param request - instance of InputStream 
    * @return xmlStr - the decompressed request XML 
    */
 
   public String decompress ( InputStream request )  
    {  
     StringBuffer xmlStr = null; 
     String str = null; 
     GZIPInputStream gzip = null; 
     BufferedReader zipReader = null; 
     try 
      {  
       gzip = new GZIPInputStream ( request ) ; 
       zipReader = new BufferedReader ( new InputStreamReader ( gzip )  ) ; 
       char chars [  ]  = new char [ 1024 ] ; 
       int len = 0; 
       xmlStr = new StringBuffer (  ) ; 
       //Write chunks of characters to the StringBuffer 
       while  (  ( len = zipReader.read ( chars, 0, chars.length )  )   > = 0 )   
        {  
         xmlStr.append ( chars, 0, len ) ; 
        }  
       chars = null; 
      }  
     catch ( IOException ioExc )  
      {  
       System.out.println ( "Exception Occured in Catch Block"+ioExc.getMessage (  )  ) ; 
      }  
     finally 
      {  
       try 
        {  
         gzip.close (  ) ; 
         zipReader.close (  ) ; 
        }  
       catch ( IOException ioExc )  
        {  
         System.out.println ( "Exception Occured in finally block"+ioExc.getMessage (  )  ) ; 
        }  
      }  
     return xmlStr.toString (  ) ; 
    }  
   } 


public GZIPInputStream(InputStream in,
                       int size)
                throws IOException
See Also:
IllegalArgumentException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public static final int GZIP_MAGIC
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public int read(byte[] buf,
                int off,
                int len)
         throws IOException
See Also:
FilterInputStream.in, InflaterInputStream
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[1907]Read and decompose Tar Gzip file
By Anonymous on 2007/08/05 20:16:33  Rate
import extended byorg.apache.commons.compress.tar.TarInputStream 
 import org.apache.commons.compress.tar.TarEntry 
  
  
 public static InputStream getInputStream ( String tarFileName )  throws Exception {  
       if ( tarFileName.substring ( tarFileName.lastIndexOf ( "." )  + 1, tarFileName.lastIndexOf ( "." )  + 3 ) .equalsIgnoreCase ( "gz" )  )  {  
          System.out.println ( "Creating an GZIPInputStream for the file" ) ; 
          return new GZIPInputStream ( new FileInputStream ( new File ( tarFileName )  )  ) ; 
        } else {  
          System.out.println ( "Creating an InputStream for the file" ) ; 
          return new FileInputStream ( new File ( tarFileName )  ) ; 
        }  
     }  
   
    public static void readTar ( InputStream in, String untarDir )  throws IOException {  
       System.out.println ( "Reading TarInputStream...  ( using classes from http://www.trustice.com/java/tar/ ) " ) ; 
       TarInputStream tin = new TarInputStream ( in ) ; 
       TarEntry tarEntry = tin.getNextEntry (  ) ; 
       if ( new File ( untarDir ) .exists (  )  )  {  
         while  ( tarEntry != null )  {  
            File destPath = new File ( untarDir + File.separatorChar + tarEntry.getName (  )  ) ; 
            System.out.println ( "Processing " + destPath.getAbsoluteFile (  )  ) ; 
            if ( !tarEntry.isDirectory (  )  )  {  
               FileOutputStream fout = new FileOutputStream ( destPath ) ; 
               tin.copyEntryContents ( fout ) ; 
               fout.close (  ) ; 
             } else {  
               destPath.mkdir (  ) ; 
             }  
            tarEntry = tin.getNextEntry (  ) ; 
          }  
         tin.close (  ) ; 
        } else {  
          System.out.println ( "That destination directory doesn't exist! " + untarDir ) ; 
        }  
     }  
  
  
 

Popular Tags