Java Zip Folder zipFolder(final File directory, final String path, final ZipOutputStream out)

Here you can find the source of zipFolder(final File directory, final String path, final ZipOutputStream out)

Description

zip Folder

License

LGPL

Declaration

private static void zipFolder(final File directory, final String path, final ZipOutputStream out)
            throws IOException 

Method Source Code


//package com.java2s;
/*/*from w  w w .  ja  v  a  2 s.  c  om*/
 *                  Eoulsan development code
 *
 * This code may be freely distributed and modified under the
 * terms of the GNU Lesser General Public License version 2.1 or
 * later and CeCILL-C. This should be distributed with the code.
 * If you do not have a copy, see:
 *
 *      http://www.gnu.org/licenses/lgpl-2.1.txt
 *      http://www.cecill.info/licences/Licence_CeCILL-C_V1-en.txt
 *
 * Copyright for this code is held jointly by the Genomic platform
 * of the Institut de Biologie de l'?cole normale sup?rieure and
 * the individual authors. These should be listed in @author doc
 * comments.
 *
 * For more information on the Eoulsan project and its aims,
 * or to join the Eoulsan Google group, visit the home page
 * at:
 *
 *      http://outils.genomique.biologie.ens.fr/eoulsan
 *
 */

import java.io.BufferedInputStream;

import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;

import java.io.IOException;

import java.util.zip.ZipEntry;

import java.util.zip.ZipOutputStream;

public class Main {
    /** The default size of the buffer. */
    private static final int DEFAULT_BUFFER_SIZE = 1024 * 4;

    private static void zipFolder(final File directory, final String path, final ZipOutputStream out)
            throws IOException {

        // Add directory even empty
        if (!"".equals(path)) {
            out.putNextEntry(new ZipEntry(path));
        }

        // Get the list of files to add
        final File[] filesToAdd = directory.listFiles(new FileFilter() {
            @Override
            public boolean accept(final File file) {
                return file.isFile();
            }
        });

        // Add the files
        if (filesToAdd != null) {

            final byte data[] = new byte[DEFAULT_BUFFER_SIZE];
            BufferedInputStream origin = null;

            for (final File f : filesToAdd) {
                out.putNextEntry(new ZipEntry(path + f.getName()));
                final FileInputStream fi = new FileInputStream(f);

                origin = new BufferedInputStream(fi, DEFAULT_BUFFER_SIZE);

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

                origin.close();
            }
        }

        // Get the list of directories to add
        final File[] directoriesToAdd = directory.listFiles(new FileFilter() {
            @Override
            public boolean accept(final File file) {

                return file.isDirectory();
            }
        });

        // Add directories
        if (directoriesToAdd != null) {
            for (final File dir : directoriesToAdd) {
                zipFolder(dir, path + File.separator + dir.getName() + File.separator, out);
            }
        }

    }
}

Related

  1. zipFolder(File root)
  2. zipFolder(File root)
  3. zipFolder(File source, File destination)
  4. zipFolder(File srcFolder, File destZipFile, boolean zipOnlySrcFolderContentsAndNotSrcFolder)
  5. zipFolder(File srcFolder, String destZipFile)
  6. zipFolder(final File srcFolder, final OutputStream out)
  7. zipFolder(final String srcFolder, final String destZipFile)
  8. zipFolder(String sourceDir, String destDir, String name)
  9. zipFolder(String sourceFolder, String target)