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