Java Zip File zip(File toZip, File outFile)

Here you can find the source of zip(File toZip, File outFile)

Description

Zips the specified file or folder

License

Open Source License

Parameter

Parameter Description
toZip a parameter
outFile a parameter

Exception

Parameter Description
IOException an exception

Declaration

public static void zip(File toZip, File outFile) throws IOException 

Method Source Code

//package com.java2s;
/**//from   ww w.j av a 2 s . c om
 * OrbisGIS is a GIS application dedicated to scientific spatial simulation.
 * This cross-platform GIS is developed at French IRSTV institute and is able to
 * manipulate and create vector and raster spatial information.
 *
 * OrbisGIS is distributed under GPL 3 license. It is produced by the "Atelier SIG"
 * team of the IRSTV Institute <http://www.irstv.fr/> CNRS FR 2488.
 *
 * Copyright (C) 2007-2012 IRSTV (FR CNRS 2488)
 *
 * This file is part of OrbisGIS.
 *
 * OrbisGIS 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.
 *
 * OrbisGIS 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
 * OrbisGIS. If not, see <http://www.gnu.org/licenses/>.
 *
 * For more information, please consult: <http://www.orbisgis.org/>
 * or contact directly:
 * info_at_ orbisgis.org
 */

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;

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

import java.io.IOException;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.zip.ZipEntry;

import java.util.zip.ZipOutputStream;

public class Main {
    private static final int BUF_SIZE = 1024 * 64;

    /**
     * Zips the specified file or folder
     *
     * @param toZip
     * @param outFile
     * @throws IOException
     */
    public static void zip(File toZip, File outFile) throws IOException {
        ZipOutputStream out = null;
        try {
            out = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(outFile)));

            byte[] data = new byte[BUF_SIZE];
            ArrayList<File> listToZip = new ArrayList<File>();
            listToZip.add(toZip);

            while (listToZip.size() > 0) {
                File file = listToZip.remove(0);
                if (file.isDirectory()) {
                    File[] children = file.listFiles();
                    listToZip.addAll(Arrays.asList(children));
                } else {
                    BufferedInputStream in = null;
                    try {
                        in = new BufferedInputStream(new FileInputStream(file), BUF_SIZE);

                        out.putNextEntry(new ZipEntry(getRelativePath(toZip, file)));
                        int count = in.read(data, 0, BUF_SIZE);
                        while (count != -1) {
                            out.write(data, 0, count);
                            count = in.read(data, 0, BUF_SIZE);
                        }
                        out.closeEntry(); // close each entry
                    } finally {
                        if (in != null) {
                            in.close();
                        }
                    }
                }
            }
            out.flush();
        } finally {
            if (out != null) {
                out.close();
            }
        }
    }

    /**
     * get the relative path to file,  according to the path to base
     * @param base
     * @param file
     * @return
     */
    public static String getRelativePath(File base, File file) {
        String absolutePath = file.getAbsolutePath();
        String path = absolutePath.substring(base.getAbsolutePath().length());
        while (path.startsWith("/")) {
            path = path.substring(1);
        }
        return path;
    }
}

Related

  1. zip(File src, File target)
  2. zip(File srcDir, File zipFile)
  3. zip(File srcDirectory, File destFile)
  4. zip(File srcFile, File destFile, String archiveRoot)
  5. zip(File theFileToZip)
  6. zip(File[] sourceFiles, File destFile, boolean deleteOriginalFiles)
  7. zip(final File dir, final File target)
  8. zip(final File tempLocation, final File targetZipFile)
  9. zip(final File zipFile, final File... files)