Java Zip Directory zip(File directory, File file)

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

Description

Recursively packs a directory into a zip file.

License

LGPL

Parameter

Parameter Description
directory The directory to zip
file The zip file

Exception

Parameter Description
IOException In case of a reading, writing or compression error

Declaration

public static final void zip(File directory, File file) throws IOException 

Method Source Code

//package com.java2s;
/**/*from  w  w  w.ja v a 2 s.c  o m*/
 * Copyright 2009-2016 Three Crickets LLC.
 * <p>
 * The contents of this file are subject to the terms of the LGPL version 3.0:
 * http://www.gnu.org/copyleft/lesser.html
 * <p>
 * Alternatively, you can obtain a royalty free commercial license with less
 * limitations, transferable or non-transferable, directly from Three Crickets
 * at http://threecrickets.com/
 */

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.io.InputStream;

import java.io.OutputStream;

import java.util.zip.ZipEntry;

import java.util.zip.ZipOutputStream;

public class Main {
    /**
     * Recursively packs a directory into a zip file.
     * 
     * @param directory
     *        The directory to zip
     * @param file
     *        The zip file
     * @throws IOException
     *         In case of a reading, writing or compression error
     */
    public static final void zip(File directory, File file) throws IOException {
        ZipOutputStream out = new ZipOutputStream(new FileOutputStream(file));
        try {
            zip(directory, directory.getPath().length() + 1, out);
        } finally {
            out.close();
        }
    }

    /**
     * Zip a directory.
     * 
     * @param directory
     *        The directory
     * @param pathPrefixLength
     *        Number of characters to strip from beginning of entry path
     * @param out
     *        Output stream
     * @throws IOException
     *         In case of a reading, writing or compression error
     */
    private static void zip(File directory, int pathPrefixLength, ZipOutputStream out) throws IOException {
        File[] files = directory.listFiles();
        for (File file : files) {
            if (file.isDirectory())
                zip(file, pathPrefixLength, out);
            else {
                ZipEntry entry = new ZipEntry(file.getPath().substring(pathPrefixLength));
                out.putNextEntry(entry);

                FileInputStream in = new FileInputStream(file);
                try {
                    copyStream(in, out);
                } finally {
                    in.close();
                }
            }
        }
    }

    /**
     * Copies streams. The input stream is entirely consumed and closed.
     * 
     * @param in
     *        Input stream
     * @param out
     *        Output stream
     * @throws IOException
     *         In case of a reading or writing error
     */
    public static void copyStream(InputStream in, OutputStream out) throws IOException {
        in = new BufferedInputStream(in);
        try {
            out = new BufferedOutputStream(out);
            try {
                while (true) {
                    int data = in.read();
                    if (data == -1)
                        break;
                    out.write(data);
                }
            } finally {
                out.flush();
            }
        } finally {
            in.close();
        }
    }
}

Related

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