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

org.javafmi.kernel.Zipper Maven / Gradle / Ivy

Go to download

javaFMI is a Java Library for the functional mock-up interface (or FMI). FMI defines a standardized interface to be used in computer simulations. The FMI Standard has beed developed by a large number of software companies and research centers that have worked in a cooperation project under the name of MODELISAR. This library addresses the connection of a java application with a FMU (functional mock-up unit).

There is a newer version: 2.26.5
Show newest version
/*
 *  Copyright 2013-2016 SIANI - ULPGC
 *
 *  This File is part of JavaFMI Project
 *
 *  JavaFMI Project is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation, either version 3 of the License.
 *
 *  JavaFMI Project 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 Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with JavaFMI. If not, see .
 */

package org.javafmi.kernel;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

import static org.javafmi.kernel.Files.*;

public class Zipper {

    public File zipDirectory(File directory, String zipName) {
        File zipFile = new File(directory.getParentFile(), zipName);
        ZipOutputStream out = new ZipOutputStream(fileOutputStream(zipFile));
        addDir(directory, out, directory.toPath());
        close(out);
        return zipFile;
    }

    private void addDir(File dirObj, ZipOutputStream out, Path basePath) {
        try {
            for(File file : dirObj.listFiles()) {
                if(file.isDirectory()) {
                    putNextEntry(out, new ZipEntry(relativizePath(basePath, file).replace(File.separatorChar, '/') + "/"));
                    addDir(file, out, basePath);
                    out.closeEntry();
                    continue;
                }
                putNextEntry(out, new ZipEntry(relativizePath(basePath, file).replace(File.separatorChar, '/')));
                BufferedInputStream source = bufferedInputStream(fileInputStream(file));
                copyFromTo(source, out);
                out.closeEntry();
                close(source);
            }
        } catch(IOException e) {
            e.printStackTrace();
        }
    }

    private String relativizePath(Path basePath, File file) {
        return basePath.relativize(file.toPath()).toString();
    }

    private void putNextEntry(ZipOutputStream out, ZipEntry zipEntry) {
        try {
            out.putNextEntry(zipEntry);
        } catch(IOException e) {
            throw new RuntimeException(e);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy