001/* 002 * (C) Copyright 2006-2017 Nuxeo (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 * Kevin Leturc <[email protected]> 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.lang3.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:[email protected]">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.isNotEmpty(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( 083 "When unziping the destination file must be a directory: " + 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}