Here you can find the source of zip(String outputFileName, String inputFileName)
public static Boolean zip(String outputFileName, String inputFileName)
//package com.java2s; //License from project: Open Source License import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; public class Main { public static Boolean zip(String outputFileName, String inputFileName) { ZipOutputStream out;//from ww w . j a va 2 s . co m try { out = new ZipOutputStream(new FileOutputStream(outputFileName)); File f = new File(inputFileName); if (f.exists()) { if (f.isDirectory()) { File[] fl = f.listFiles(); for (File element : fl) { out.putNextEntry(new ZipEntry(element.getName())); FileInputStream in = new FileInputStream(element); byte[] buffer = new byte[1024]; int b = 0; while ((b = in.read(buffer)) != -1) { out.write(buffer, 0, b); out.flush(); } in.close(); } } else if (f.isFile()) { out.putNextEntry(new ZipEntry(f.getName())); FileInputStream in = new FileInputStream(f); byte[] buffer = new byte[1024]; int b = 0; while ((b = in.read(buffer)) != -1) { out.write(buffer, 0, b); out.flush(); } in.close(); } } else { return false; } out.close(); } catch (Exception e) { e.printStackTrace(); return false; } return true; } }