public static File addPrjFileToArchive ( File file , String targetCrs ) throws ZipException , IOException , NoSuchAuthorityCodeException , FactoryException { ZipFile zipFile = new ZipFile ( file ) ; CoordinateReferenceSystem decodedTargetCrs = CRS . decode ( targetCrs ) ; String targetCrsWkt = toSingleLineWKT ( decodedTargetCrs ) ; ArrayList < String > zipFileNames = new ArrayList < String > ( ) ; List < FileHeader > zipFileHeaders = zipFile . getFileHeaders ( ) ; for ( FileHeader zipFileHeader : zipFileHeaders ) { if ( FilenameUtils . getExtension ( zipFileHeader . getFileName ( ) ) . equalsIgnoreCase ( "prj" ) ) { continue ; } zipFileNames . add ( FilenameUtils . getBaseName ( zipFileHeader . getFileName ( ) ) ) ; } LOG . debug ( "Following files will be created and added to ZIP file: " + zipFileNames ) ; for ( String prefix : zipFileNames ) { File targetPrj = null ; try { targetPrj = File . createTempFile ( "TMP_" + prefix , ".prj" ) ; FileUtils . write ( targetPrj , targetCrsWkt , "UTF-8" ) ; ZipParameters params = new ZipParameters ( ) ; params . setSourceExternalStream ( true ) ; params . setCompressionLevel ( Zip4jConstants . DEFLATE_LEVEL_NORMAL ) ; params . setFileNameInZip ( prefix + ".prj" ) ; zipFile . addFile ( targetPrj , params ) ; } finally { if ( targetPrj != null ) { targetPrj . delete ( ) ; } } } return zipFile . getFile ( ) ; }
Add a projection file to a shapefile zip archive .