Here you can find the source of addFileToZip(File root, File file, ZipOutputStream zos)
private static void addFileToZip(File root, File file, ZipOutputStream zos) throws IOException
//package com.java2s; //License from project: Apache License import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; public class Main { private static void addFileToZip(File root, File file, ZipOutputStream zos) throws IOException { FileInputStream fis = new FileInputStream(file); String filePath = file.getCanonicalPath(); String zipFilePath = filePath.substring(root.getCanonicalPath().length() + 1, filePath.length()); ZipEntry zipEntry = new ZipEntry(zipFilePath); zos.putNextEntry(zipEntry);/*from w w w . j ava2 s.com*/ byte[] bytes = new byte[1024]; int length; while ((length = fis.read(bytes)) >= 0) { zos.write(bytes, 0, length); } zos.closeEntry(); fis.close(); } }