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.apache.commons.lang.StringUtils; 029import org.nuxeo.common.utils.Path; 030import org.nuxeo.common.utils.PathFilter; 031import org.nuxeo.common.utils.ZipUtils; 032import org.nuxeo.runtime.deployment.preprocessor.install.Command; 033import org.nuxeo.runtime.deployment.preprocessor.install.CommandContext; 034 035/** 036 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a> 037 */ 038public class UnzipCommand implements Command { 039 040 protected final Path src; 041 042 protected final Path dst; 043 044 protected final PathFilter filter; 045 046 protected final String prefix; 047 048 public UnzipCommand(Path src, Path dst) { 049 this(src, dst, null, null); 050 } 051 052 public UnzipCommand(Path src, Path dst, PathFilter filter) { 053 this(src, dst, filter, null); 054 } 055 056 public UnzipCommand(Path src, Path dst, String prefix) { 057 this(src, dst, null, prefix); 058 } 059 060 public UnzipCommand(Path src, Path dst, PathFilter filter, String prefix) { 061 this.src = src; 062 this.dst = dst; 063 this.filter = filter; 064 this.prefix = prefix; 065 } 066 067 @Override 068 public void exec(CommandContext ctx) throws IOException { 069 File baseDir = ctx.getBaseDir(); 070 File srcFile = new File(baseDir, ctx.expandVars(src.toString())); 071 File dstFile = new File(baseDir, ctx.expandVars(dst.toString())); 072 073 if (!srcFile.exists()) { 074 throw new FileNotFoundException("Could not find the file " + srcFile.getAbsolutePath() + " to unzip."); 075 } 076 077 if (srcFile.isDirectory()) { 078 Path p = !StringUtils.isEmpty(prefix) ? new Path("/" + prefix) : new Path("/"); 079 new CopyCommand(src.addTrailingSeparator(), dst.addTrailingSeparator(), p, filter).exec(ctx); 080 return; 081 } 082 083 if (dstFile.isFile()) { 084 throw new IllegalArgumentException("When unziping the destination file must be a directory: " 085 + dstFile.getAbsolutePath()); 086 } 087 088 if (!dstFile.exists()) { 089 dstFile.mkdirs(); 090 } 091 // unzip srcFile to directory dstFile 092 if (filter != null) { 093 if (prefix != null) { 094 ZipUtils.unzip(prefix, srcFile, dstFile, filter); 095 } else { 096 ZipUtils.unzip(srcFile, dstFile, filter); 097 } 098 } else { 099 if (prefix != null) { 100 ZipUtils.unzip(prefix, srcFile, dstFile); 101 } else { 102 ZipUtils.unzip(srcFile, dstFile); 103 } 104 } 105 } 106 107 @Override 108 public String toString() { 109 return "unzip " + src.toString() + " > " + dst.toString(); 110 } 111 112 @Override 113 public String toString(CommandContext ctx) { 114 return "unzip " + ctx.expandVars(src.toString()) + " > " + ctx.expandVars(dst.toString()); 115 } 116 117}