Here you can find the source of zipFile(File file, String zipFile)
public static void zipFile(File file, String zipFile) throws Exception
//package com.java2s; /******************************************************************************* * Copyright (c) 2016 Chen Chao(cnfree2000@hotmail.com). * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors:/*from w w w . j av a 2 s . c o m*/ * Chen Chao - initial API and implementation *******************************************************************************/ import java.io.BufferedInputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStream; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; public class Main { public static void zipFile(File file, String zipFile) throws Exception { ZipOutputStream zos = new ZipOutputStream(new FileOutputStream( zipFile)); ZipEntry ze = null; byte[] buf = new byte[1024]; int readLen = 0; ze = new ZipEntry(file.getName()); ze.setSize(file.length()); ze.setTime(file.lastModified()); zos.putNextEntry(ze); InputStream is = new BufferedInputStream(new FileInputStream(file)); while ((readLen = is.read(buf, 0, 1024)) != -1) { zos.write(buf, 0, readLen); } is.close(); zos.close(); } }