public static void zip(File input,String pathWithinArchive,File output) throws IOException {
  try (ZipOutputStream zipStream=new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(output)))){
    zipStream.setMethod(ZipOutputStream.DEFLATED);
    zipStream.setLevel(9);
    zipStream.putNextEntry(new ZipEntry(pathWithinArchive));
    try (BufferedInputStream inputStream=new BufferedInputStream(new FileInputStream(input))){
      copy(inputStream,zipStream);
    }
   }
 }

public static void zip ( File input , String pathWithinArchive , File output ) throws IOException { try ( ZipOutputStream zipStream = new ZipOutputStream ( new BufferedOutputStream ( new FileOutputStream ( output ) ) ) ) { zipStream . setMethod ( ZipOutputStream . DEFLATED ) ; zipStream . setLevel ( 9 ) ; zipStream . putNextEntry ( new ZipEntry ( pathWithinArchive ) ) ; try ( BufferedInputStream inputStream = new BufferedInputStream ( new FileInputStream ( input ) ) ) { copy ( inputStream , zipStream ) ; } } }
creates a single - entry zip file .
