Here you can find the source of zipFiles(final File outputFile, final File[] files)
Parameter | Description |
---|---|
outputFile | Output file |
files | File to add to the zip |
Parameter | Description |
---|---|
IOException | if an error occurs while creating the zip file |
public static void zipFiles(final File outputFile, final File[] files) throws IOException
//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(); } }