public static PackableObject identifyByHeader ( File file , List packables ) throws IOException { FileInputStream fis = null ; try { /* Archive result */ // PackableObject packable = null; // identify archive by header fis = new FileInputStream ( file ) ; byte [ ] headerBytes = new byte [ 20 ] ; fis . read ( headerBytes , 0 , 20 ) ; Iterator iter = packables . iterator ( ) ; while ( iter . hasNext ( ) ) { PackableObject p = ( PackableObject ) iter . next ( ) ; byte [ ] fieldHeader = p . getHeader ( ) ; if ( fieldHeader != null ) { if ( compareByteArrays ( headerBytes , fieldHeader ) ) { return p ; } } } // if we couldn't find an archiver by header bytes, we'll give it a // try // with the default name extension. This is useful, cause some // archives // like tar have no header. String name = file . getName ( ) ; String extension = null ; String [ ] s = name . split ( "\\." ) ; if ( s . length > 1 ) { extension = s [ s . length - 1 ] ; } Iterator it = packables . iterator ( ) ; while ( it . hasNext ( ) ) { PackableObject p = ( PackableObject ) it . next ( ) ; if ( p . isPackableWith ( extension , PackableObject . CHOOSE_EXTENSION ) ) { return p ; } } // No implementation found return null ; } finally { IOUtils . closeQuietly ( fis ) ; } }
Compares a file to a list of packables and identifies an object by header . If no matching header is found it identifies the file by file extension . If identification was not successfull null is returned