Here you can find the source of zipFile(ZipOutputStream zout, File file, int rootLength)
private static synchronized void zipFile(ZipOutputStream zout, File file, int rootLength) throws Exception
//package com.java2s; /*--------------------------------------------------------------- * Copyright 2005 by the Radiological Society of North America * * This source software is released under the terms of the * RSNA Public License (http://mirc.rsna.org/rsnapubliclicense) *----------------------------------------------------------------*/ import java.io.File; import java.io.FileInputStream; import java.io.InputStream; import java.io.OutputStream; import java.io.Reader; import java.io.Writer; import java.util.jar.JarFile; import java.util.zip.ZipEntry; import java.util.zip.ZipFile; import java.util.zip.ZipOutputStream; public class Main { private static synchronized void zipFile(ZipOutputStream zout, File file, int rootLength) throws Exception { FileInputStream fin;/*from www . j a v a 2 s . c o m*/ ZipEntry ze; byte[] buffer = new byte[10000]; int bytesread; String entryname = file.getAbsolutePath().substring(rootLength); entryname = entryname.replaceAll("\\\\", "/"); ze = new ZipEntry(entryname); if (file.exists()) { fin = new FileInputStream(file); zout.putNextEntry(ze); while ((bytesread = fin.read(buffer)) > 0) zout.write(buffer, 0, bytesread); zout.closeEntry(); fin.close(); } } /** * Close an InputStream and ignore Exceptions. * @param stream the stream to close. */ public static void close(InputStream stream) { if (stream != null) { try { stream.close(); } catch (Exception ignore) { } } } /** * Close an OutputStream and ignore Exceptions. * @param stream the stream to close. */ public static void close(OutputStream stream) { if (stream != null) { try { stream.close(); } catch (Exception ignore) { } } } /** * Close a Writer and ignore Exceptions. * @param writer the writer to close. */ public static void close(Writer writer) { if (writer != null) { try { writer.close(); } catch (Exception ignore) { } } } /** * Close a Reader and ignore Exceptions. * @param reader the reader to close. */ public static void close(Reader reader) { if (reader != null) { try { reader.close(); } catch (Exception ignore) { } } } /** * Close a JarFile and ignore Exceptions. * @param jarFile the JarFile to close. */ public static void close(JarFile jarFile) { if (jarFile != null) { try { jarFile.close(); } catch (Exception ignore) { } } } /** * Close a ZipFile and ignore Exceptions. * @param zipFile the zipFile to close. */ public static void close(ZipFile zipFile) { if (zipFile != null) { try { zipFile.close(); } catch (Exception ignore) { } } } }