ZipFiles.java [src/csip/utils] Revision: 445c5e807c8bf0fabd9f52f0e394eced6b4cc91e  Date: Wed Apr 26 16:46:23 MDT 2017
/*
 * $Id$
 *
 * This file is part of the Cloud Services Integration Platform (CSIP),
 * a Model-as-a-Service framework, API and application suite.
 *
 * 2012-2017, Olaf David and others, OMSLab, Colorado State University.
 *
 * OMSLab licenses this file to you under the MIT license.
 * See the LICENSE file in the project root for more information.
 */
package csip.utils;

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.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;

/**
 * basic zip/unzip operations.
 *
 * @author od
 */
public class ZipFiles {

    private static final int BUFF = 4096 * 2;


    /**
     *
     * @param dir
     * @return the zipped file.
     * @throws IOException
     */
    public static File zip(File dir) throws IOException {
        return zip(dir, new File(dir.getParentFile(), dir.getName() + ".zip"));
    }


    /**
     *
     * @param dir
     * @param zipFile
     * @return the zipped file
     * @throws IOException
     */
    public static File zip(File dir, File zipFile) throws IOException {
        ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zipFile));
        zos.setLevel(9);
        byte[] buffer = new byte[BUFF];
        zip(dir, dir, zos, buffer);
        zos.close();
        return zipFile;
    }


    /**
     *
     * @param dir
     * @param base
     * @param zos
     * @param buffer
     * @throws IOException
     */
    private static void zip(File dir, File base, ZipOutputStream zos, byte[] buffer) throws IOException {
        for (File file : dir.listFiles()) {
            if (file.isDirectory()) {
                zip(file, base, zos, buffer);
            } else {
                FileInputStream in = new FileInputStream(file);
                ZipEntry ze = new ZipEntry(file.getPath().substring(base.getPath().length() + 1));
                ze.setTime(file.lastModified());
                zos.putNextEntry(ze);
                int read = 0;
                while ((read = in.read(buffer)) != -1) {
                    zos.write(buffer, 0, read);
                }
                in.close();
                zos.closeEntry();
            }
        }
    }


    /**
     * Unzip a file.
     * 
     * @param zip the file to unzip.
     * @return the directory
     * @throws IOException
     */
    public static File unzip(File zip) throws IOException {
        return unzip(zip, zip.getParentFile());
    }


    /**
     * Unzip a file.
     * 
     * @param zip the file to unzip
     * @param dir the directory.
     * @return the directory
     * @throws IOException
     */
    public static File unzip(File zip, File dir) throws IOException {
        byte[] buffer = new byte[BUFF];
        ZipFile archive = new ZipFile(zip);
        Enumeration<? extends ZipEntry> e = archive.entries();
        while (e.hasMoreElements()) {
            ZipEntry entry = e.nextElement();
            File file = new File(dir, entry.getName());
            if (entry.isDirectory()) {
                continue;
            }
            if (!file.getParentFile().exists()) {
                file.getParentFile().mkdirs();
            }
            InputStream in = archive.getInputStream(entry);
            BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(file));
            int read;
            while ((read = in.read(buffer)) != -1) {
                out.write(buffer, 0, read);
            }
            in.close();
            out.close();
            if (entry.getTime() != -1) {
                file.setLastModified(entry.getTime());
            }
        }
        return dir;
    }


    public static void main(String[] args) throws IOException {
//        zipDir(new File("/home/od/Documents"), new File("/tmp/docs.zip"));
        unzip(new File("/tmp/b.zip"), new File("/tmp"));
    }
}