All Downloads are FREE. Search and download functionalities are using the official Maven repository.

csip.utils.ZipFiles Maven / Gradle / Ivy

Go to download

The Cloud Services Integration Platform is a SoA implementation to offer a Model-as-a-Service framework, Application Programming Interface, deployment infrastructure, and service implementations for environmental modeling.

There is a newer version: 2.6.30
Show newest version
/*
 * $Id: ZipFiles.java c4b9d8c126c7 2020-07-28 [email protected] $
 *
 * This file is part of the Cloud Services Integration Platform (CSIP),
 * a Model-as-a-Service framework, API and application suite.
 *
 * 2012-2019, 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;


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


    /**
     * Append directory to zip file
     *
     * @param dir the input directory
     * @param zipFile the zip file to append dir to
     * @return the zipped file
     * @throws IOException required by ZipOutputStream and zip method
     */
    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 required by unzip method
     */
    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 required by ZipFile, BufferedOutputStream, InputStream
     */
    public static File unzip(File zip, File dir) throws IOException {
        byte[] buffer = new byte[BUFF];
        ZipFile archive = new ZipFile(zip);
        Enumeration 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;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy