Java Zip File zip(File destination, File... files)

Here you can find the source of zip(File destination, File... files)

Description

Zips a set of files in one file.

License

Open Source License

Parameter

Parameter Description
destination File to contain the zipped files.
files Files to be zipped.

Exception

Parameter Description
IOException an exception

Declaration

public static void zip(File destination, File... files) throws IOException 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;

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 BUFFER = 2048;

    /**//from   w  w w .  ja  v  a 2  s .  c  o m
     * Zips a set of files in one file.
     * 
     * @param destination File to contain the zipped files.
     * @param files Files to be zipped.
     * @throws IOException 
     */
    public static void zip(File destination, File... files) throws IOException {

        BufferedInputStream origin = null;
        FileOutputStream dest = new FileOutputStream(destination);

        try (ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(dest))) {

            byte data[] = new byte[BUFFER];

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

                FileInputStream fi = new FileInputStream(files[i]);
                origin = new BufferedInputStream(fi, BUFFER);
                ZipEntry entry = new ZipEntry(files[i].getName());
                out.putNextEntry(entry);
                int count;

                while ((count = origin.read(data, 0, BUFFER)) != -1) {
                    out.write(data, 0, count);
                }

                origin.close();

            }

        }

    }
}

Related

  1. zip(File f, File destfile)
  2. zip(File f, File out)
  3. zip(File file, File output)
  4. zip(File file, File zip)