public static String unzipFolder ( String zipFile , String destFolder , boolean addTimeStamp ) throws IOException { String newFirstName = null ; try ( ZipFile zf = new ZipFile ( zipFile ) ) { Enumeration < ? extends ZipEntry > zipEnum = zf . entries ( ) ; String firstName = null ; while ( zipEnum . hasMoreElements ( ) ) { ZipEntry item = ( ZipEntry ) zipEnum . nextElement ( ) ; String itemName = item . getName ( ) ; if ( firstName == null ) { int firstSlash = itemName . indexOf ( ' ' ) ; if ( firstSlash == - 1 ) { firstSlash = itemName . length ( ) ; } firstName = itemName . substring ( 0 , firstSlash ) ; newFirstName = firstName ; File baseFile = new File ( destFolder + File . separator + firstName ) ; if ( baseFile . exists ( ) ) { if ( addTimeStamp ) { newFirstName = firstName + "_" + new DateTime ( ) . toString ( HMConstants . dateTimeFormatterYYYYMMDDHHMMSScompact ) ; } else { throw new IOException ( "Not overwriting existing: " + baseFile ) ; } } } if ( firstName == null ) { throw new IOException ( ) ; } itemName = itemName . replaceFirst ( firstName , newFirstName ) ; if ( item . isDirectory ( ) ) { File newdir = new File ( destFolder + File . separator + itemName ) ; if ( ! newdir . mkdir ( ) ) throw new IOException ( ) ; } else { String newfilePath = destFolder + File . separator + itemName ; File newFile = new File ( newfilePath ) ; File parentFile = newFile . getParentFile ( ) ; if ( ! parentFile . exists ( ) ) { if ( ! parentFile . mkdirs ( ) ) throw new IOException ( ) ; } InputStream is = zf . getInputStream ( item ) ; FileOutputStream fos = new FileOutputStream ( newfilePath ) ; byte [ ] buffer = new byte [ 512 ] ; int readchars = 0 ; while ( ( readchars = is . read ( buffer ) ) != - 1 ) { fos . write ( buffer , 0 , readchars ) ; } is . close ( ) ; fos . close ( ) ; } } } return newFirstName ; }
Uncompress a compressed file to the contained structure .