public static void pack(final File sourceDir,final File targetZip,final int compressionLevel) throws IOException {
  Preconditions.checkArgument(sourceDir != null && sourceDir.exists() && sourceDir.isDirectory(),"Source directory is not exist: %s",sourceDir.getAbsolutePath());
  long startTime=System.currentTimeMillis();
  ZipOutputStream zipOut=null;
  try {
    zipOut=new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(targetZip)));
    zipOut.setLevel(compressionLevel);
    pack(sourceDir,sourceDir.getParent() + File.separator,zipOut);
  }
  finally {
    IOUtils.closeQuietly(zipOut);
    long interval=(System.currentTimeMillis() - startTime) / 1000;
    if (interval >= DELAY_THRESHOLD_IN_SECOND) {
      long folderSize=FileUtils.sizeOfDirectory(sourceDir);
      log.info(String.format("Zip folder: %s from %d to %d bytes, took %s seconds",sourceDir.getAbsolutePath(),folderSize,targetZip.length(),interval));
    }
  }
}

public static void pack ( final File sourceDir , final File targetZip , final int compressionLevel ) throws IOException { Preconditions . checkArgument ( sourceDir != null && sourceDir . exists ( ) && sourceDir . isDirectory ( ) , "Source directory is not exist: %s" , sourceDir . getAbsolutePath ( ) ) ; long startTime = System . currentTimeMillis ( ) ; ZipOutputStream zipOut = null ; try { zipOut = new ZipOutputStream ( new BufferedOutputStream ( new FileOutputStream ( targetZip ) ) ) ; zipOut . setLevel ( compressionLevel ) ; pack ( sourceDir , sourceDir . getParent ( ) + File . separator , zipOut ) ; } finally { IOUtils . closeQuietly ( zipOut ) ; long interval = ( System . currentTimeMillis ( ) - startTime ) / 1000 ; if ( interval >= DELAY_THRESHOLD_IN_SECOND ) { long folderSize = FileUtils . sizeOfDirectory ( sourceDir ) ; log . info ( String . format ( "Zip folder: %s from %d to %d bytes, took %s seconds" , sourceDir . getAbsolutePath ( ) , folderSize , targetZip . length ( ) , interval ) ) ; } } }
compresses the given directory and all its sub - directories into a zip file with specified compress level . < p > the zip file must not be a directory .
