Here you can find the source of addFilesToZip(File zipFile, File[] files, String[] fileNames)
public static void addFilesToZip(File zipFile, File[] files, String[] fileNames) throws IOException
//package com.java2s; /**/*from w w w.ja v a2 s . co m*/ * odt2braille - Braille authoring in OpenOffice.org. * * Copyright (c) 2010-2011 by DocArch <http://www.docarch.be>. * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser 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 Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ import java.io.File; import java.io.IOException; import java.io.InputStream; import java.io.FileOutputStream; import java.io.FileInputStream; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; import java.util.zip.ZipOutputStream; public class Main { public static void addFilesToZip(File zipFile, File[] files, String[] fileNames) throws IOException { File tempFile = new File(zipFile.getAbsoluteFile() + ".temp"); if (!zipFile.renameTo(tempFile)) { throw new RuntimeException("could not rename"); } byte[] buf = new byte[1024]; ZipInputStream zin = new ZipInputStream(new FileInputStream( tempFile)); ZipOutputStream out = new ZipOutputStream(new FileOutputStream( zipFile)); ZipEntry entry = zin.getNextEntry(); while (entry != null) { String name = entry.getName(); boolean notInFiles = true; for (String f : fileNames) { if (f.equals(name)) { notInFiles = false; break; } } if (notInFiles) { out.putNextEntry(new ZipEntry(name)); int len; while ((len = zin.read(buf)) > 0) { out.write(buf, 0, len); } } entry = zin.getNextEntry(); } zin.close(); for (int i = 0; i < files.length; i++) { InputStream in = new FileInputStream(files[i]); out.putNextEntry(new ZipEntry(fileNames[i])); int len; while ((len = in.read(buf)) > 0) { out.write(buf, 0, len); } out.closeEntry(); in.close(); } out.close(); tempFile.delete(); } }