001/*
002 * (C) Copyright 2006-2007 Nuxeo SAS (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Nuxeo - initial API and implementation
016 *
017 * $Id$
018 */
019
020package org.nuxeo.runtime.deployment.preprocessor.install.commands;
021
022import java.io.File;
023import java.io.FileNotFoundException;
024import java.io.IOException;
025
026import org.apache.commons.lang.StringUtils;
027import org.nuxeo.common.utils.Path;
028import org.nuxeo.common.utils.PathFilter;
029import org.nuxeo.common.utils.ZipUtils;
030import org.nuxeo.runtime.deployment.preprocessor.install.Command;
031import org.nuxeo.runtime.deployment.preprocessor.install.CommandContext;
032
033/**
034 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
035 */
036public class UnzipCommand implements Command {
037
038    protected final Path src;
039
040    protected final Path dst;
041
042    protected final PathFilter filter;
043
044    protected final String prefix;
045
046    public UnzipCommand(Path src, Path dst) {
047        this(src, dst, null, null);
048    }
049
050    public UnzipCommand(Path src, Path dst, PathFilter filter) {
051        this(src, dst, filter, null);
052    }
053
054    public UnzipCommand(Path src, Path dst, String prefix) {
055        this(src, dst, null, prefix);
056    }
057
058    public UnzipCommand(Path src, Path dst, PathFilter filter, String prefix) {
059        this.src = src;
060        this.dst = dst;
061        this.filter = filter;
062        this.prefix = prefix;
063    }
064
065    @Override
066    public void exec(CommandContext ctx) throws IOException {
067        File baseDir = ctx.getBaseDir();
068        File srcFile = new File(baseDir, ctx.expandVars(src.toString()));
069        File dstFile = new File(baseDir, ctx.expandVars(dst.toString()));
070
071        if (!srcFile.exists()) {
072            throw new FileNotFoundException("Could not find the file " + srcFile.getAbsolutePath() + " to unzip.");
073        }
074
075        if (srcFile.isDirectory()) {
076            Path p = !StringUtils.isEmpty(prefix) ? new Path("/" + prefix) : new Path("/");
077            new CopyCommand(src.addTrailingSeparator(), dst.addTrailingSeparator(), p, filter).exec(ctx);
078            return;
079        }
080
081        if (dstFile.isFile()) {
082            throw new IllegalArgumentException("When unziping the destination file must be a directory: "
083                    + dstFile.getAbsolutePath());
084        }
085
086        if (!dstFile.exists()) {
087            dstFile.mkdirs();
088        }
089        // unzip srcFile to directory dstFile
090        if (filter != null) {
091            if (prefix != null) {
092                ZipUtils.unzip(prefix, srcFile, dstFile, filter);
093            } else {
094                ZipUtils.unzip(srcFile, dstFile, filter);
095            }
096        } else {
097            if (prefix != null) {
098                ZipUtils.unzip(prefix, srcFile, dstFile);
099            } else {
100                ZipUtils.unzip(srcFile, dstFile);
101            }
102        }
103    }
104
105    @Override
106    public String toString() {
107        return "unzip " + src.toString() + " > " + dst.toString();
108    }
109
110    @Override
111    public String toString(CommandContext ctx) {
112        return "unzip " + ctx.expandVars(src.toString()) + " > " + ctx.expandVars(dst.toString());
113    }
114
115}