Java Zip Directory zip(File directory, File zipFile)

Here you can find the source of zip(File directory, File zipFile)

Description

Zip the contents of a directory

License

Open Source License

Declaration

public static void zip(File directory, File zipFile) throws IOException 

Method Source Code

//package com.java2s;
/**/*from  w  w w  . j a va2s  .  co m*/
 * e-Science Central
 * Copyright (C) 2008-2013 School of Computing Science, Newcastle University
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * version 2 as published by the Free Software Foundation at:
 * http://www.gnu.org/licenses/gpl-2.0.html
 *
 * 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, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, 5th Floor, Boston, MA 02110-1301, USA.
 */

import java.io.*;
import java.util.zip.*;

public class Main {
    /** Zip the contents of a directory */
    public static void zip(File directory, File zipFile) throws IOException {
        ZipOutputStream zipStream = null;
        try {
            zipStream = new ZipOutputStream(new FileOutputStream(zipFile));
            zipDir(directory.getPath(), directory.getPath(), zipStream);
        } catch (IOException ioe) {
            throw ioe;
        } finally {
            try {
                zipStream.flush();
            } catch (Exception e) {
            }

            try {
                zipStream.close();
            } catch (Exception e) {
            }
        }
    }

    /** Zip a directory to a zip output stream */
    public static void zipDir(String baseDir, String dir2zip, ZipOutputStream zos) throws IOException {

        //create a new File object based on the directory we have to zip
        File zipDir = new File(dir2zip);
        //get a listing of the directory content
        String[] dirList = zipDir.list();
        byte[] readBuffer = new byte[2156];
        int bytesIn = 0;
        //loop through dirList, and zip the files
        for (int i = 0; i < dirList.length; i++) {
            File f = new File(zipDir, dirList[i]);
            if (f.isDirectory()) {
                //if the File object is a directory, call this
                //function again to add its content recursively
                String filePath = f.getPath();
                zipDir(baseDir, filePath, zos);
                //loop again
                continue;
            }
            //if we reached here, the File object f was not a directory
            //create a FileInputStream on top of f
            FileInputStream fis = new FileInputStream(f);
            // create a    new zipentry
            ZipEntry anEntry = new ZipEntry(subtractPath(baseDir, f.getPath()).replace("\\", "/"));

            //place the zip entry in the ZipOutputStream object
            zos.putNextEntry(anEntry);
            //now write the content of the file to the ZipOutputStream
            while ((bytesIn = fis.read(readBuffer)) != -1) {
                zos.write(readBuffer, 0, bytesIn);
            }
            //close the Stream
            fis.close();
        }
    }

    public static String subtractPath(String base, String full) {
        return full.substring(base.length() + 1);
    }
}

Related

  1. zip(File dir2zip, File zipFile)
  2. zip(File directory, File base, ZipOutputStream zos)
  3. zip(File directory, File base, ZipOutputStream zos, byte[] buffer)
  4. zip(File directory, File file)
  5. zip(File directory, File zipFile)
  6. zipDirectories(List directories, File baseDirectory, File zipFile)
  7. zipDirectory(File baseDirectory, File output)
  8. zipDirectory(File dir, boolean includeDirInZip, boolean includeHidden)
  9. zipDirectory(File dir, File zipFile)