public static void addNormalizerToModel ( File f , Normalizer < ? > normalizer ) { File tempFile = null ; try { // copy existing model to temporary file tempFile = DL4JFileUtils . createTempFile ( "dl4jModelSerializerTemp" , "bin" ) ; tempFile . deleteOnExit ( ) ; Files . copy ( f , tempFile ) ; try ( ZipFile zipFile = new ZipFile ( tempFile ) ; ZipOutputStream writeFile = new ZipOutputStream ( new BufferedOutputStream ( new FileOutputStream ( f ) ) ) ) { // roll over existing files within model, and copy them one by one Enumeration < ? extends ZipEntry > entries = zipFile . entries ( ) ; while ( entries . hasMoreElements ( ) ) { ZipEntry entry = entries . nextElement ( ) ; // we're NOT copying existing normalizer, if any if ( entry . getName ( ) . equalsIgnoreCase ( NORMALIZER_BIN ) ) continue ; log . debug ( "Copying: {}" , entry . getName ( ) ) ; InputStream is = zipFile . getInputStream ( entry ) ; ZipEntry wEntry = new ZipEntry ( entry . getName ( ) ) ; writeFile . putNextEntry ( wEntry ) ; IOUtils . copy ( is , writeFile ) ; } // now, add our normalizer as additional entry ZipEntry nEntry = new ZipEntry ( NORMALIZER_BIN ) ; writeFile . putNextEntry ( nEntry ) ; NormalizerSerializer . getDefault ( ) . write ( normalizer , writeFile ) ; } } catch ( Exception ex ) { throw new RuntimeException ( ex ) ; } finally { if ( tempFile != null ) { tempFile . delete ( ) ; } } }
This method appends normalizer to a given persisted model .