Java Zip Folder zipFolder(File source, File destination)

Here you can find the source of zipFolder(File source, File destination)

Description

This method allows you to zip a folder.

License

Open Source License

Parameter

Parameter Description
source folder to zip
destination target zip file

Exception

Parameter Description
IOException in case of failure

Declaration

public static void zipFolder(File source, File destination) throws IOException 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2008-2011 Chair for Applied Software Engineering,
 * Technische Universitaet Muenchen.//  w ww .ja  va2s  .  c om
 * 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:
 ******************************************************************************/

import java.io.BufferedOutputStream;
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 allows you to zip a folder. *UNDER CONSTRUCTION*
     * 
     * @param source folder to zip
     * @param destination target zip file
     * @throws IOException in case of failure
     */
    public static void zipFolder(File source, File destination) throws IOException {
        if (!source.isDirectory()) {
            throw new IOException("Source must be folder.");
        }
        if (destination.exists()) {
            throw new IOException("Destination already exists.");
        }
        ZipOutputStream zipOutputStream = new ZipOutputStream(
                new BufferedOutputStream(new FileOutputStream(destination)));
        String path = source.getPath();
        path += (path.endsWith(File.separator) ? "" : File.separatorChar);
        zip(source, path, zipOutputStream, new byte[8192]);
        zipOutputStream.close();
    }

    private static void zip(File current, String rootPath, ZipOutputStream zipStream, byte[] buffer)
            throws IOException {
        if (current.isDirectory()) {
            for (File file : current.listFiles()) {
                if (!".".equals(file.getName()) && !"..".equals(file.getName())) {
                    zip(file, rootPath, zipStream, buffer);
                }
            }
        } else if (current.isFile()) {
            zipStream.putNextEntry(new ZipEntry(current.getPath().replace(rootPath, "")));
            FileInputStream file = new FileInputStream(current);
            int read;
            while ((read = file.read(buffer)) != -1) {
                zipStream.write(buffer, 0, read);
            }
            zipStream.closeEntry();
        } else {
            throw new IllegalStateException();
        }
    }
}

Related

  1. zipDir(String dir2zip, ZipOutputStream zos)
  2. zipDir(String directory, String zipName)
  3. zipDir(String origDir, File dirObj, ZipOutputStream zos)
  4. zipFolder(File root)
  5. zipFolder(File root)
  6. zipFolder(File srcFolder, File destZipFile, boolean zipOnlySrcFolderContentsAndNotSrcFolder)
  7. zipFolder(File srcFolder, String destZipFile)
  8. zipFolder(final File directory, final String path, final ZipOutputStream out)
  9. zipFolder(final File srcFolder, final OutputStream out)