Here you can find the source of zipFiles(File output, File root, List
private static void zipFiles(File output, File root, List<File> fileList)
//package com.java2s; //License from project: Apache License import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.util.List; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; public class Main { private static void zipFiles(File output, File root, List<File> fileList) { try {//from w w w . j av a2s . c om FileOutputStream fos = new FileOutputStream(output); ZipOutputStream zos = new ZipOutputStream(fos); for (File file : fileList) { if (file.isFile()) { addFileToZip(root, file, zos); } } zos.close(); fos.close(); } catch (IOException e) { throw new RuntimeException("Unable to write zip file.", e); } } 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); byte[] bytes = new byte[1024]; int length; while ((length = fis.read(bytes)) >= 0) { zos.write(bytes, 0, length); } zos.closeEntry(); fis.close(); } }