Java Zip Folder zipDir(File zipDir, ZipOutputStream zos, boolean recurse)

Here you can find the source of zipDir(File zipDir, ZipOutputStream zos, boolean recurse)

Description

Utility to compress the contents of a Directory to a ZipOutputStream, if necessary recursing

License

Open Source License

Parameter

Parameter Description
zipDir the origin directory
zos the ZipOutputStream output
recurse should we recurse in subdirs

Exception

Parameter Description
IOException an exception

Declaration


public static void zipDir(File zipDir, ZipOutputStream zos,
        boolean recurse) throws IOException 

Method Source Code

//package com.java2s;
/*******************************************************************************
 *
 *  * Copyright (C) 2007, 2010 - The University of Liverpool
 *  * This program is free software; you can redistribute it and/or modify it under the terms
 *  * of the GNU General Public License as published by the Free Software Foundation; either version 3 of the License,
 *  * or (at your option) any later version.
 *  * This program 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 General Public License for more details.
 *  * You should have received a copy of the GNU General Public License along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *  *//w w  w  .j  a va 2  s . com
 *  * Author: Fabio Corubolo
 *  * Email: corubolo@gmail.com
 * 
 *******************************************************************************/

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

import java.io.IOException;

import java.util.zip.ZipEntry;

import java.util.zip.ZipOutputStream;

public class Main {
    /** Utility to compress the contents of a Directory to a ZipOutputStream, if necessary recursing
     * 
     * @param zipDir the origin directory
     * @param zos the ZipOutputStream output
     * @param recurse should we recurse in subdirs
     * @throws IOException
     */

    public static void zipDir(File zipDir, ZipOutputStream zos,
            boolean recurse) throws IOException {
        String[] dirList = zipDir.list();
        byte[] readBuffer = new byte[2156];
        int bytesIn = 0;
        for (String element : dirList) {
            File f = new File(zipDir, element);
            if (f.isDirectory()) {
                if (recurse)
                    zipDir(f, zos, recurse);
                // loop again
                continue;
            }
            FileInputStream fis = new FileInputStream(f);
            String en;
            if (recurse)
                en = f.getPath();
            else
                en = f.getName();
            ZipEntry anEntry = new ZipEntry(en);
            zos.putNextEntry(anEntry);
            while ((bytesIn = fis.read(readBuffer)) != -1)
                zos.write(readBuffer, 0, bytesIn);
            fis.close();
        }

    }
}

Related

  1. zipDir(File dir, String relativePath, ZipOutputStream zos)
  2. zipDir(File dir, String relativePath, ZipOutputStream zos, boolean start)
  3. zipDir(File dir, ZipOutputStream zos, String prefix)
  4. zipDir(File directory, ZipOutputStream zos, String path, Set exclusions)
  5. zipDir(File sourceDir, File destFile)
  6. zipDir(File zipDir, ZipOutputStream zos, String archiveSourceDir)
  7. zipDir(File zipDir, ZipOutputStream zos, String basePath)
  8. zipDir(File zipDir, ZipOutputStream zos, String name)
  9. zipDir(File zipFile, File dir, boolean includeRoot)