Java Zip Files addToZip(String[] sourceFiles, ZipOutputStream output)

Here you can find the source of addToZip(String[] sourceFiles, ZipOutputStream output)

Description

add To Zip

License

Open Source License

Declaration

private static void addToZip(String[] sourceFiles, ZipOutputStream output)
            throws FileNotFoundException, IOException 

Method Source Code

//package com.java2s;
/**//from  w w  w.j ava 2 s  .co  m
 * Aptana Studio
 * Copyright (c) 2005-2012 by Appcelerator, Inc. All Rights Reserved.
 * Licensed under the terms of the GNU Public License (GPL) v3 (with exceptions).
 * Please see the license.html included with this distribution for details.
 * Any modifications to this file must keep this entire header intact.
 */

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

import java.io.IOException;

import java.util.zip.ZipOutputStream;

public class Main {
    private static void addToZip(String[] sourceFiles, ZipOutputStream output)
            throws FileNotFoundException, IOException {
        byte[] buffer = new byte[1024];
        for (String file : sourceFiles) {
            File content = new File(file);
            if (content.isDirectory()) {
                File[] children = content.listFiles();
                String[] childrenPaths = new String[children.length];
                for (int i = 0; i < children.length; i++) {
                    childrenPaths[i] = children[i].getAbsolutePath();
                }

                addToZip(childrenPaths, output);
            } else if (content.canRead()) {
                FileInputStream input = new FileInputStream(file);
                output.putNextEntry(new java.util.zip.ZipEntry(file));

                int length;
                while ((length = input.read(buffer)) > 0) {
                    output.write(buffer, 0, length);
                }

                output.closeEntry();
                input.close();
            }
        }
    }
}

Related

  1. addToZip(File f, int truncate, ZipOutputStream os, byte[] buff)
  2. addToZip(File file, ZipOutputStream out, String folder)
  3. addToZip(File input, String entryName, ZipOutputStream zos, boolean withHidden, Set excludeEntries)
  4. addToZip(String path, String srcFile, ZipOutputStream zip)
  5. addToZip(String path, String srcFile, ZipOutputStream zip)
  6. addToZip(ZipOutputStream zos, String rootDirectoryName, String fileName)
  7. addToZipFile(InputStream source, String entryName, ZipOutputStream zos)
  8. addToZipFile(String fileName, ZipOutputStream zos)
  9. fileToZip(File file, File zipFile)