Java Zip File List zipFiles(final File outputFile, final File[] files)

Here you can find the source of zipFiles(final File outputFile, final File[] files)

Description

Create a zip file.

License

GNU General Public License

Parameter

Parameter Description
outputFile Output file
files File to add to the zip

Exception

Parameter Description
IOException if an error occurs while creating the zip file

Declaration

public static void zipFiles(final File outputFile, final File[] files) throws IOException 

Method Source Code

//package com.java2s;
/*//w  w w.j  a v a  2  s  . c  o m
 *                      Nividic development code
 *
 * This code may be freely distributed and modified under the
 * terms of the GNU Lesser General Public Licence.  This should
 * be distributed with the code.  If you do not have a copy,
 * see:
 *
 *      http://www.gnu.org/copyleft/lesser.html
 *
 * Copyright for this code is held jointly by the microarray platform
 * of the ?cole Normale Sup?rieure and the individual authors.
 * These should be listed in @author doc comments.
 *
 * For more information on the Nividic project and its aims,
 * or to join the Nividic mailing list, visit the home page
 * at:
 *
 *      http://www.transcriptome.ens.fr/nividic
 *
 */

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class Main {
    private static final int ZIP_BUFFER = 1024;

    /**
     * Create a zip file.
     * @param outputFile Output file
     * @param files File to add to the zip
     * @throws IOException if an error occurs while creating the zip file
     */
    public static void zipFiles(final File outputFile, final File[] files) throws IOException {

        if (outputFile == null || files == null)
            return;

        // Create a buffer for reading the files
        final byte[] buf = new byte[ZIP_BUFFER];

        // Create the ZIP file
        ZipOutputStream out = new ZipOutputStream(new FileOutputStream(outputFile));

        // Compress the files
        for (int i = 0; i < files.length; i++) {

            final File f = files[i];

            if (f == null)
                continue;

            FileInputStream in = new FileInputStream(f);

            // Add ZIP entry to output stream.
            out.putNextEntry(new ZipEntry(f.getName()));

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

        out.close();
    }
}

Related

  1. zipFiles(File file, JarOutputStream jos, String pathName)
  2. zipFiles(File output, File root, List fileList)
  3. zipFiles(File rootDir, File currDir, ZipOutputStream zos)
  4. zipFiles(File[] dirList, ZipOutputStream zos)
  5. zipFiles(File[] listFiles2Zip, File output)
  6. zipFiles(List files, File output)
  7. zipFiles(List files, OutputStream os)
  8. zipFiles(List srcfile, File zipfile)
  9. ZipFiles(String destination, Vector files)