Java Zip File List zipFiles(List files, OutputStream os)

Here you can find the source of zipFiles(List files, OutputStream os)

Description

Zip a set of files.

License

Open Source License

Parameter

Parameter Description
files List <File/> list of files to be zipped
os OutputStream outputstream where the zip is created

Exception

Parameter Description
IOException an exception

Declaration

public static void zipFiles(List<File> files, OutputStream os) throws IOException 

Method Source Code

//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();
            }
        }

    }
}

Related

  1. zipFiles(File rootDir, File currDir, ZipOutputStream zos)
  2. zipFiles(File[] dirList, ZipOutputStream zos)
  3. zipFiles(File[] listFiles2Zip, File output)
  4. zipFiles(final File outputFile, final File[] files)
  5. zipFiles(List files, File output)
  6. zipFiles(List srcfile, File zipfile)
  7. ZipFiles(String destination, Vector files)
  8. zipFiles(String filename, String[] files)
  9. zipFiles(String files[], String fielPath)