001/*
002 * (C) Copyright 2006-2007 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Nuxeo - initial API and implementation
018 *
019 * $Id$
020 */
021
022package org.nuxeo.runtime.deployment.preprocessor.install.commands;
023
024import java.io.File;
025import java.io.FileNotFoundException;
026import java.io.IOException;
027
028import org.nuxeo.common.utils.Path;
029import org.nuxeo.common.utils.PathFilter;
030import org.nuxeo.common.utils.ZipUtils;
031import org.nuxeo.runtime.deployment.preprocessor.install.Command;
032import org.nuxeo.runtime.deployment.preprocessor.install.CommandContext;
033
034/**
035 * Zip the content of a directory.
036 * <p>
037 * A prefix may be specified.
038 *
039 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
040 */
041public class ZipCommand implements Command {
042
043    protected final Path src;
044
045    protected final Path dst;
046
047    protected final PathFilter filter;
048
049    protected final String prefix;
050
051    public ZipCommand(Path src, Path dst) {
052        this(src, dst, null, null);
053    }
054
055    public ZipCommand(Path src, Path dst, String prefix) {
056        this(src, dst, prefix, null);
057    }
058
059    public ZipCommand(Path src, Path dst, String prefix, PathFilter filter) {
060        this.src = src;
061        this.dst = dst;
062        this.prefix = prefix;
063        this.filter = filter;
064    }
065
066    @Override
067    public void exec(CommandContext ctx) throws IOException {
068        File baseDir = ctx.getBaseDir();
069        File srcFile = new File(baseDir, ctx.expandVars(src.toString()));
070        File dstFile = new File(baseDir, ctx.expandVars(dst.toString()));
071
072        if (!srcFile.exists()) {
073            throw new FileNotFoundException("Could not find the file " + srcFile.getAbsolutePath() + " to zip.");
074        }
075
076        if (dstFile.isDirectory()) {
077            throw new IllegalArgumentException("When ziping the destination file must be a file: "
078                    + dstFile.getAbsolutePath());
079        }
080
081        File parent = dstFile.getParentFile();
082        if (!parent.exists()) {
083            parent.mkdirs();
084        }
085
086        if (srcFile.isDirectory()) {
087            // unzip only thre directory content
088            // the prefix can be used to add the root directory itself
089            File[] files = srcFile.listFiles();
090            // zip files to directory dstFile
091            if (filter != null) {
092                ZipUtils.zip(files, dstFile, prefix);
093                // TODO: add filter capabilities
094            } else {
095                ZipUtils.zip(files, dstFile, prefix);
096            }
097        } else {
098            // zip srcFiles to directory dstFile
099            if (filter != null) {
100                ZipUtils.zip(srcFile, dstFile, prefix);
101                // TODO: add filter capabilities
102            } else {
103                ZipUtils.zip(srcFile, dstFile, prefix);
104            }
105        }
106    }
107
108    @Override
109    public String toString() {
110        return "zip " + src.toString() + " > " + dst.toString();
111    }
112
113    @Override
114    public String toString(CommandContext ctx) {
115        return "zip " + ctx.expandVars(src.toString()) + " > " + ctx.expandVars(dst.toString());
116    }
117
118}