Here you can find the source of zip(List
public static void zip(List<String> fileNames, String outFileName)
//package com.java2s; /*//from w w w .java2 s .co m * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import java.io.*; import java.util.List; import java.util.zip.ZipOutputStream; import java.util.zip.ZipEntry; public class Main { public static void zip(List<String> fileNames, String outFileName) { // Create a buffer for reading the files byte[] buf = new byte[1024]; try { // Create the ZIP file ZipOutputStream out = new ZipOutputStream(new FileOutputStream(outFileName)); // Compress the files for (String fileName : fileNames) { FileInputStream in = new FileInputStream(fileName); // Add ZIP entry to output stream. out.putNextEntry(new ZipEntry(fileName)); // Transfer bytes from the file to the ZIP file int len; while ((len = in.read(buf)) > 0) { out.write(buf, 0, len); } // Complete the entry out.closeEntry(); in.close(); } // Complete the ZIP file out.close(); } catch (IOException e) { e.printStackTrace(); } } public static void zip(List<String> fileNames, String outFileName, String withoutBase) { // Create a buffer for reading the files byte[] buf = new byte[1024]; try { // Create the ZIP file ZipOutputStream out = new ZipOutputStream(new FileOutputStream(outFileName)); // Compress the files for (String fileName : fileNames) { FileInputStream in = new FileInputStream(fileName); // Add ZIP entry to output stream. if (withoutBase != null) { fileName = fileName.substring(withoutBase.length() + 1); } out.putNextEntry(new ZipEntry(fileName)); // Transfer bytes from the file to the ZIP file int len; while ((len = in.read(buf)) > 0) { out.write(buf, 0, len); } // Complete the entry out.closeEntry(); in.close(); } // Complete the ZIP file out.close(); } catch (IOException e) { e.printStackTrace(); } } }