Here you can find the source of zip(List
public static ZipOutputStream zip(List<String> srcFiles, OutputStream os) throws IOException
//package com.java2s; /**// www . j ava2s. co m * Copyright 2016 Milinda Pathirage * <p> * Licensed 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 * <p> * http://www.apache.org/licenses/LICENSE-2.0 * <p> * 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.ZipEntry; import java.util.zip.ZipOutputStream; public class Main { public static ZipOutputStream zip(List<String> srcFiles, OutputStream os) throws IOException { // create byte buffer byte[] buffer = new byte[1024]; ZipOutputStream zos = new ZipOutputStream(os); for (String f : srcFiles) { File srcFile = new File(f); FileInputStream fis = new FileInputStream(srcFile); // begin writing a new ZIP entry, positions the stream to the start of the entry data zos.putNextEntry(new ZipEntry(srcFile.getName())); int length; while ((length = fis.read(buffer)) > 0) { zos.write(buffer, 0, length); } zos.closeEntry(); // close the InputStream fis.close(); } return zos; } }