Here you can find the source of zipFiles(List
Parameter | Description |
---|---|
files | List <File/> list of files to be zipped |
os | OutputStream outputstream where the zip is created |
Parameter | Description |
---|---|
IOException | an exception |
public static void zipFiles(List<File> files, OutputStream os) throws IOException
//package com.java2s; /*//from w w w. j a v a2 s .c o m * * Copyright (C) 2013 * * This file is part of Messic. * * 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.FileInputStream; import java.io.IOException; import java.io.OutputStream; import java.util.List; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; public class Main { /** * Zip a set of files. The results is sent to an {@link OutputStream} * * @param files {@link List}<File/> list of files to be zipped * @param os {@link OutputStream} outputstream where the zip is created * @throws IOException */ public static void zipFiles(List<File> files, OutputStream os) throws IOException { FileInputStream in = null; ZipOutputStream zos = new ZipOutputStream(os); for (File file : files) { ZipEntry ze = new ZipEntry(file.getName()); zos.putNextEntry(ze); try { byte[] buffer = new byte[1024]; in = new FileInputStream(file.getAbsolutePath()); int len; while ((len = in.read(buffer)) > 0) { zos.write(buffer, 0, len); } } finally { in.close(); } } } }