Java Zip Directory zipDirectory(File directory, File zip)

Here you can find the source of zipDirectory(File directory, File zip)

Description

This method converts a directory into a zip file

License

Open Source License

Parameter

Parameter Description
directory The directory to zip
zip The destination zip file

Exception

Parameter Description
IOException an exception

Declaration

public static final void zipDirectory(File directory, File zip) throws IOException 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2004, 2010 BREDEX GmbH.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:/*  w  ww  . j  a  va 2s.c om*/
 *     BREDEX GmbH - initial API and implementation and/or initial documentation
 *******************************************************************************/

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 {
    /**
     * This method converts a directory into a zip file
     * @param directory The directory to zip
     * @param zip The destination zip file
     * @throws IOException
     */
    public static final void zipDirectory(File directory, File zip) throws IOException {
        ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zip));
        zip(directory, directory, zos);
        zos.close();
    }

    /**
     * This method converts a directory into a zip file
     * @param directory The directory to zip
     * @param base The directory to zip
     * @param zos A ZipOutputStream
     * @throws IOException
     */
    private static final void zip(File directory, File base, ZipOutputStream zos) throws IOException {
        File[] files = directory.listFiles();
        byte[] buffer = new byte[8192];
        int read = 0;
        for (int i = 0, n = files.length; i < n; i++) {
            if (files[i].isDirectory()) {
                zip(files[i], base, zos);
            } else {
                FileInputStream in = new FileInputStream(files[i]);
                ZipEntry entry = new ZipEntry(files[i].getPath().substring(base.getPath().length() + 1));
                zos.putNextEntry(entry);
                while (-1 != (read = in.read(buffer))) {
                    zos.write(buffer, 0, read);
                }
                in.close();
            }
        }
    }
}

Related

  1. zipDirectory(File baseDirectory, File output)
  2. zipDirectory(File dir, boolean includeDirInZip, boolean includeHidden)
  3. zipDirectory(File dir, File zipFile)
  4. zipDirectory(File dir, String base, ZipOutputStream zout)
  5. zipDirectory(File dir, String zipDirName)
  6. zipDirectory(File directory, File zip)
  7. zipDirectory(File directory, File zipFile)
  8. zipDirectory(File directory, File zipFile)
  9. zipDirectory(File directory, File zipFile, Pattern exclusion)