Here you can find the source of zip(File destination, File... files)
Parameter | Description |
---|---|
destination | File to contain the zipped files. |
files | Files to be zipped. |
Parameter | Description |
---|---|
IOException | an exception |
public static void zip(File destination, File... files) throws IOException
//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(); } } } }