Here you can find the source of zip(String path, String zipFilePath)
public static void zip(String path, String zipFilePath) throws IOException
//package com.java2s; //License from project: Open Source License import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; public class Main { public static void zip(String path, String zipFilePath) throws IOException { FileOutputStream fos = new FileOutputStream(zipFilePath); ZipOutputStream zos = new ZipOutputStream(fos); File file = new File(path); putFileToZipEntry(zos, file, file.getName()); zos.close();/*from ww w.java 2s. c o m*/ } private static void putFileToZipEntry(ZipOutputStream zos, File file, String path) throws IOException { //File file = new File(path); if (file.isFile()) { ZipEntry entry = new ZipEntry(path); zos.putNextEntry(entry); FileInputStream fis = new FileInputStream(file); byte[] buffer = new byte[4096]; int len; while ((len = fis.read(buffer)) > 0) { zos.write(buffer, 0, len); } fis.close(); zos.closeEntry(); } else if (file.isDirectory()) { File[] subFiles = file.listFiles(); //String[] subFiles = file.list(); for (File subFile : subFiles) { putFileToZipEntry(zos, subFile, path + File.separator + subFile.getName()); } } //ignore other kind of files } }