Java Zip Folder zipDir(File zipDir, ZipOutputStream zos, String basePath)

Here you can find the source of zipDir(File zipDir, ZipOutputStream zos, String basePath)

Description

zip Dir

License

Open Source License

Declaration

private static void zipDir(File zipDir, ZipOutputStream zos, String basePath) throws Exception 

Method Source Code

//package com.java2s;
/*/*from   w w  w. ja  v a2s.co  m*/
 * JBoss, Home of Professional Open Source.
 * Copyright 2011, Red Hat, Inc., and individual contributors
 * as indicated by the @author tags.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */

import java.io.File;

import java.io.FileInputStream;

import java.io.IOException;
import java.io.InputStream;

import java.util.zip.ZipEntry;

import java.util.zip.ZipOutputStream;

public class Main {
    private static final int BUFFER_SIZE = 2048;

    private static void zipDir(File zipDir, ZipOutputStream zos, String basePath) throws Exception {
        FileInputStream fis = null;
        try {
            File[] dirList = zipDir.listFiles();
            byte[] readBuffer = new byte[BUFFER_SIZE];
            int bytesIn = 0;
            for (int i = 0; i < dirList.length; i++) {
                File f = dirList[i];
                if (f.isDirectory()) {
                    zipDir(f, zos, basePath);
                } else {
                    fis = new FileInputStream(f);
                    ZipEntry anEntry = new ZipEntry(f.getPath().replaceFirst(basePath, ""));
                    zos.putNextEntry(anEntry);
                    while ((bytesIn = fis.read(readBuffer)) != -1) {
                        zos.write(readBuffer, 0, bytesIn);
                    }
                    fis.close();
                    fis = null;
                }
            }
        } finally {
            closeInputStream(fis);
        }
    }

    /**
     * Safely close input stream.
     * 
     * @param is
     */
    public static void closeInputStream(InputStream is) {
        if (is != null) {
            try {
                is.close();
            } catch (IOException e) {
            }
        }
    }
}

Related

  1. zipDir(File dir, ZipOutputStream zos, String prefix)
  2. zipDir(File directory, ZipOutputStream zos, String path, Set exclusions)
  3. zipDir(File sourceDir, File destFile)
  4. zipDir(File zipDir, ZipOutputStream zos, boolean recurse)
  5. zipDir(File zipDir, ZipOutputStream zos, String archiveSourceDir)
  6. zipDir(File zipDir, ZipOutputStream zos, String name)
  7. zipDir(File zipFile, File dir, boolean includeRoot)
  8. zipDir(File zipFile, File dirObj)
  9. zipDir(final String dir2zip, final ZipOutputStream zos, final String root)