Java Zip Folder zipFolder(String srcFolder, String destZipFile, boolean addBaseFolder)

Here you can find the source of zipFolder(String srcFolder, String destZipFile, boolean addBaseFolder)

Description

Compress a folder and its contents.

License

Open Source License

Parameter

Parameter Description
srcFolder path to the folder to be compressed.
destZipFile path to the final output zip file.
addBaseFolder flag to decide whether to add also the provided base folder or not.

Declaration

public static void zipFolder(String srcFolder, String destZipFile, boolean addBaseFolder) throws IOException 

Method Source Code


//package com.java2s;
/*// ww w.java2  s  . c  o  m
 * JGrass - Free Open Source Java GIS http://www.jgrass.org 
 * (C) HydroloGIS - www.hydrologis.com 
 *
 * This program 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 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 Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

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 {
    /**
     * Compress a folder and its contents.
     * 
     * @param srcFolder path to the folder to be compressed.
     * @param destZipFile path to the final output zip file.
     * @param addBaseFolder flag to decide whether to add also the provided base folder or not.
     */
    public static void zipFolder(String srcFolder, String destZipFile, boolean addBaseFolder) throws IOException {
        if (new File(srcFolder).isDirectory()) {

            ZipOutputStream zip = null;
            FileOutputStream fileWriter = null;
            try {
                fileWriter = new FileOutputStream(destZipFile);
                zip = new ZipOutputStream(fileWriter);
                addFolderToZip("", srcFolder, zip, addBaseFolder); //$NON-NLS-1$
            } finally {
                if (zip != null) {
                    zip.flush();
                    zip.close();
                }
                if (fileWriter != null)
                    fileWriter.close();
            }
        } else
            throw new IOException("The base file is supposed to be a directory.");
    }

    private static void addFolderToZip(String path, String srcFolder, ZipOutputStream zip, boolean addFolder)
            throws IOException {
        File folder = new File(srcFolder);
        String listOfFiles[] = folder.list();
        for (int i = 0; i < listOfFiles.length; i++) {
            String folderPath = null;
            if (path.length() < 1) {
                folderPath = folder.getName();
            } else {
                folderPath = path + File.separator + folder.getName();
            }
            String srcFile = srcFolder + File.separator + listOfFiles[i];
            addToZip(folderPath, srcFile, zip);
        }
    }

    private static void addToZip(String path, String srcFile, ZipOutputStream zip) throws IOException {
        File folder = new File(srcFile);
        if (folder.isDirectory()) {
            addFolderToZip(path, srcFile, zip, true);
        } else {
            byte[] buf = new byte[1024];
            int len;
            FileInputStream in = null;
            try {
                in = new FileInputStream(srcFile);
                zip.putNextEntry(new ZipEntry(path + File.separator + folder.getName()));
                while ((len = in.read(buf)) > 0) {
                    zip.write(buf, 0, len);
                }
            } finally {
                if (in != null)
                    in.close();
            }
        }
    }
}

Related

  1. zipFolder(String sourceDir, String destDir, String name)
  2. zipFolder(String sourceFolder, String target)
  3. zipFolder(String srcFolder, String destZipFile)
  4. zipFolder(String srcFolder, String destZipFile)
  5. zipFolder(String srcFolder, String destZipFile)
  6. zipFolder(String srcFolderPath, String outputZipPath)
  7. zipFolder(ZipOutputStream zipOut, File folder, File root)
  8. zipFolder2(ZipOutputStream zos, String folderName, String baseFolderName)