Here you can find the source of zip(File srcFile, File targetFile)
public static boolean zip(File srcFile, File targetFile)
//package com.java2s; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; public class Main { public static boolean zip(File srcFile, File targetFile) { if (!srcFile.exists()) return false; if (!targetFile.getParentFile().exists()) targetFile.getParentFile().mkdir(); try {/* w w w.ja va 2s . c o m*/ FileInputStream localFileInputStream = new FileInputStream( targetFile); ZipOutputStream localZipOutputStream = new ZipOutputStream( new FileOutputStream(targetFile)); byte[] arrayOfByte = new byte[1024]; localZipOutputStream.putNextEntry(new ZipEntry(srcFile .getName())); while (true) { int i = localFileInputStream.read(arrayOfByte); if (i <= 0) { localZipOutputStream.closeEntry(); localFileInputStream.close(); localZipOutputStream.close(); return true; } localZipOutputStream.write(arrayOfByte, 0, i); } } catch (Exception localException) { localException.printStackTrace(); } return false; } }