Here you can find the source of zip(String zipFileName, String[] zipEntries)
public static void zip(String zipFileName, String[] zipEntries)
//package com.java2s; //License from project: Open Source License import java.util.zip.ZipOutputStream; import java.util.zip.ZipEntry; import java.io.FileOutputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.BufferedInputStream; import java.io.FileInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.util.zip.Deflater; public class Main { public static void zip(String zipFileName, String[] zipEntries) { // Get the current directory for later use String currentDirectory = System.getProperty("user.dir"); try (ZipOutputStream zos = new ZipOutputStream( new BufferedOutputStream(new FileOutputStream(zipFileName)))) { // Set the compression level to best compression zos.setLevel(Deflater.BEST_COMPRESSION); // Add each entry to the ZIP file for (int i = 0; i < zipEntries.length; i++) { // Make sure the entry file exists File entryFile = new File(zipEntries[i]); if (!entryFile.exists()) { System.out.println("The entry file " + entryFile.getAbsolutePath() + " does not exist"); System.out.println("Aborted processing."); return; }/*from www . j av a2 s .co m*/ // Create a ZipEntry object ZipEntry ze = new ZipEntry(zipEntries[i]); // Add zip entry object to the ZIP file zos.putNextEntry(ze); // Add the contents of the entry to the ZIP file addEntryContent(zos, zipEntries[i]); // We are done with the current entry zos.closeEntry(); } System.out.println("Output has been written to " + currentDirectory + File.separator + zipFileName); } catch (IOException e) { e.printStackTrace(); } } public static void addEntryContent(ZipOutputStream zos, String entryFileName) throws IOException, FileNotFoundException { // Create an input stream to read data from the entry file BufferedInputStream bis = new BufferedInputStream(new FileInputStream(entryFileName)); byte[] buffer = new byte[1024]; int count = -1; while ((count = bis.read(buffer)) != -1) { zos.write(buffer, 0, count); } bis.close(); } }