Here you can find the source of doZip(String inFilePath, String outFilePath)
public static void doZip(String inFilePath, String outFilePath)
//package com.java2s; /*//from w ww. j a va 2 s. co m * This source code is licensed under the MIT-style license found in the * LICENSE file in the root directory of this source tree. */ import java.io.*; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; public class Main { public static void doZip(String inFilePath, String outFilePath) { try { File inFile = new File(inFilePath); ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(outFilePath)); zipFile(zipOut, inFile, ""); zipOut.close(); } catch (IOException ioe) { ioe.printStackTrace(); } } private static void zipFile(ZipOutputStream out, File file, String dir) throws IOException { if (file.isDirectory()) { out.putNextEntry(new ZipEntry(dir + "/")); dir = dir.length() == 0 ? "" : dir + "/"; File[] files = file.listFiles(); for (int i = 0; i < files.length; i++) { zipFile(out, files[i], dir + files[i].getName()); } } else { FileInputStream fis = new FileInputStream(file); out.putNextEntry(new ZipEntry(dir)); int tempByte; byte[] buffer = new byte[1024]; while ((tempByte = fis.read(buffer)) > 0) { out.write(buffer, 0, tempByte); } fis.close(); } } }