public InputStream getInputStream ( File f ) throws IOException { // use the magic numbers to determine the compression type, // use file extension only as 2nd choice int magic = 0 ; InputStream test = getInputStreamFromFile ( f ) ; magic = getMagicNumber ( test ) ; test . close ( ) ; InputStream inputStream = null ; String fileName = f . getName ( ) ; if ( magic == UncompressInputStream . LZW_MAGIC ) { // a Z compressed file return openCompressedFile ( f ) ; } else if ( magic == GZIP_MAGIC ) { return openGZIPFile ( f ) ; } else if ( fileName . endsWith ( ".gz" ) ) { return openGZIPFile ( f ) ; } else if ( fileName . endsWith ( ".zip" ) ) { ZipFile zipfile = new ZipFile ( f ) ; // stream to first entry is returned ... ZipEntry entry ; Enumeration < ? extends ZipEntry > e = zipfile . entries ( ) ; if ( e . hasMoreElements ( ) ) { entry = e . nextElement ( ) ; inputStream = zipfile . getInputStream ( entry ) ; } else { throw new IOException ( "Zip file has no entries" ) ; } } else if ( fileName . endsWith ( ".jar" ) ) { JarFile jarFile = new JarFile ( f ) ; // stream to first entry is returned JarEntry entry ; Enumeration < JarEntry > e = jarFile . entries ( ) ; if ( e . hasMoreElements ( ) ) { entry = e . nextElement ( ) ; inputStream = jarFile . getInputStream ( entry ) ; } else { throw new IOException ( "Jar file has no entries" ) ; } } else if ( fileName . endsWith ( ".Z" ) ) { // unix compressed return openCompressedFile ( f ) ; } else { // no particular extension found, assume that it is an uncompressed file inputStream = getInputStreamFromFile ( f ) ; } return inputStream ; }
Get an InputStream for the file . The caller is responsible for closing the stream or otherwise a resource leak can occur .