com.c4om.autoconf.ulysses.configanalyzer.packager.ZipConfigurationPackager.java Source code

Java tutorial

Introduction

Here is the source code for com.c4om.autoconf.ulysses.configanalyzer.packager.ZipConfigurationPackager.java

Source

/*
Copyright 2014 Universidad Politcnica de Madrid - Center for Open Middleware (http://www.centeropenmiddleware.com)
    
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
    
http://www.apache.org/licenses/LICENSE-2.0
    
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package com.c4om.autoconf.ulysses.configanalyzer.packager;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;

import com.c4om.autoconf.ulysses.configanalyzer.core.datastructures.ConfigurationPackage;

/**
 * This class packages all the configurations related to a target into respective 
 * zip files.
 * @author Pablo Alonso Rodrguez (Center for Open Middleware - UPM)
 *
 */
public class ZipConfigurationPackager extends CompressedFileConfigurationPackager implements ConfigurationPackager {

    /**
     * Constructor
     * @param rootFolder the root folder of the configuration (under the temporary directory).
     */
    public ZipConfigurationPackager(File destinationFolder, List<String> filesToInclude) {
        super(destinationFolder, filesToInclude);
    }

    /**
     * This implementation compresses files into a ZIP file
     * @see com.c4om.autoconf.ulysses.interfaces.configanalyzer.packager.CompressedFileConfigurationPackager#compressFiles(java.io.File, java.util.List, java.io.File)
     */
    @Override
    protected void compressFiles(File actualRootFolder, List<String> entries, File zipFile)
            throws FileNotFoundException, IOException {
        //We use the FileUtils method so that necessary directories are automatically created.
        FileOutputStream zipFOS = FileUtils.openOutputStream(zipFile);
        ZipOutputStream zos = new ZipOutputStream(zipFOS);
        for (String entry : entries) {
            ZipEntry ze = new ZipEntry(entry);
            zos.putNextEntry(ze);
            FileInputStream sourceFileIN = new FileInputStream(
                    actualRootFolder.getAbsolutePath() + File.separator + entry);
            IOUtils.copy(sourceFileIN, zos);
            sourceFileIN.close();
        }
        zos.closeEntry();
        zos.close();
    }

    /**
     * Our implementation creates zip files, thus the returned extension is "zip"
     * @see com.c4om.autoconf.ulysses.interfaces.configanalyzer.packager.CompressedFileConfigurationPackager#getExtension()
     */
    @Override
    protected String getExtension() {
        return ConfigurationPackage.PACKAGE_TYPE_ZIP;
    }

}