Here you can find the source of zip(File zipFile, File[] files)
public static void zip(File zipFile, File[] files)
//package com.java2s; /* //from w w w . j a v a 2 s. co m * RealmSpeak is the Java application for playing the board game Magic Realm. * Copyright (c) 2005-2015 Robin Warren * E-mail: robin@dewkid.com * * This program is free software: you can redistribute it and/or modify it under the terms of the * GNU General Public License as published by the Free Software Foundation, either version 3 of the License, * or (at your option) any later version. * * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * for more details. * * You should have received a copy of the GNU General Public License along with this program. If not, see * * http://www.gnu.org/licenses/ */ import java.io.*; import java.util.zip.*; public class Main { private static final int BUFFER = 2048; public static void zip(File zipFile, File[] files) { try { BufferedInputStream origin = null; FileOutputStream dest = new FileOutputStream(zipFile); ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(dest)); out.setMethod(ZipOutputStream.DEFLATED); byte data[] = new byte[BUFFER]; for (int i = 0; i < files.length; i++) { // System.out.println("Adding: " + files[i].getPath()); FileInputStream fi = new FileInputStream(files[i]); origin = new BufferedInputStream(fi, BUFFER); ZipEntry entry = new ZipEntry(files[i].getName()); out.putNextEntry(entry); int count; while ((count = origin.read(data, 0, BUFFER)) != -1) { out.write(data, 0, count); } origin.close(); } out.close(); } catch (Exception e) { e.printStackTrace(); } } }