Here you can find the source of zip(final File zipFile, final File[] files)
Parameter | Description |
---|---|
zipFile | contains the zip archive name |
files | files to put in the archive |
public static boolean zip(final File zipFile, final File[] files)
//package com.java2s; /*/* w w w . j a v a 2 s.c o m*/ * ################################################################ * * ProActive Parallel Suite(TM): The Java(TM) library for * Parallel, Distributed, Multi-Core Computing for * Enterprise Grids & Clouds * * Copyright (C) 1997-2011 INRIA/University of * Nice-Sophia Antipolis/ActiveEon * Contact: proactive@ow2.org or contact@activeeon.com * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Affero General Public License * as published by the Free Software Foundation; version 3 of * the License. * * This library 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 * Affero General Public License for more details. * * You should have received a copy of the GNU Affero General Public License * along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 * USA * * If needed, contact us to obtain a release under GPL Version 2 or 3 * or a different license than the AGPL. * * Initial developer(s): The ActiveEon Team * http://www.activeeon.com/ * Contributor(s): * * ################################################################ * $$ACTIVEEON_INITIAL_DEV$$ */ 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 { /** * compresses the specified files into a zip archive * * @param zipFile contains the zip archive name * @param files files to put in the archive * @return Returns true if the zip was successful, false otherwise */ public static boolean zip(final File zipFile, final File[] files) { ZipOutputStream out = null; try { byte[] buffer = new byte[4096]; // Create a buffer for copying int bytesRead; out = new ZipOutputStream(new FileOutputStream(zipFile)); for (File f : files) { FileInputStream in = null; try { if (f.isDirectory()) continue;//Ignore directory in = new FileInputStream(f); // Stream to read file ZipEntry entry = new ZipEntry(f.getPath()); // Make a ZipEntry out.putNextEntry(entry); // Store entry while ((bytesRead = in.read(buffer)) != -1) out.write(buffer, 0, bytesRead); in.close(); } finally { if (in != null) { try { in.close(); } catch (IOException e) { } } } } out.close(); } catch (Exception e) { return false; } finally { if (out != null) { try { out.close(); } catch (IOException e) { } } } return true; } /** * compresses the specified files into a zip archive * * @param zipFile contains the zip archive name * @param files files to put in the archive * @return Returns true if the zip was successful, false otherwise */ public static boolean zip(final String zipFile, final String[] files) { File[] ffiles = new File[files.length]; for (int i = 0; i < files.length; i++) { ffiles[i] = new File(files[i]); } return zip(new File(zipFile), ffiles); } }