Here you can find the source of addFileToZip(File in, File parent, ZipOutputStream out)
protected static void addFileToZip(File in, File parent, ZipOutputStream out) throws IOException
//package com.java2s; /* ******************************************************************* * Copyright (c) 1999-2000 Xerox Corporation. * All rights reserved. /*w w w. j av a2 s .co m*/ * 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: * Xerox/PARC initial implementation * ******************************************************************/ import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; public class Main { /** read a file out to the zip stream */ protected static void addFileToZip(File in, File parent, ZipOutputStream out) throws IOException { String path = in.getCanonicalPath(); String parentPath = parent.getCanonicalPath(); if (!path.startsWith(parentPath)) { throw new Error("not parent: " + parentPath + " of " + path); } else { path = path.substring(1 + parentPath.length()); path = path.replace('\\', '/'); // todo: use filesep } ZipEntry entry = new ZipEntry(path); entry.setTime(in.lastModified()); // todo: default behavior is DEFLATED out.putNextEntry(entry); InputStream input = null; try { input = new FileInputStream(in); byte[] buf = new byte[1024]; int count; while (0 < (count = input.read(buf, 0, buf.length))) { out.write(buf, 0, count); } } finally { if (null != input) input.close(); } } }