001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one
003     * or more contributor license agreements.  See the NOTICE file
004     * distributed with this work for additional information
005     * regarding copyright ownership.  The ASF licenses this file
006     * to you under the Apache License, Version 2.0 (the
007     * "License"); you may not use this file except in compliance
008     * with the License.  You may obtain a copy of the License at
009     *
010     *  http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing,
013     * software distributed under the License is distributed on an
014     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015     * KIND, either express or implied.  See the License for the
016     * specific language governing permissions and limitations
017     * under the License.
018     */
019    
020    package org.apache.geronimo.farm.deployment;
021    
022    import java.io.BufferedInputStream;
023    import java.io.BufferedOutputStream;
024    import java.io.File;
025    import java.io.FileInputStream;
026    import java.io.FileOutputStream;
027    import java.io.IOException;
028    import java.io.InputStream;
029    import java.io.OutputStream;
030    import java.util.Enumeration;
031    import java.util.zip.ZipEntry;
032    import java.util.zip.ZipFile;
033    import java.util.zip.ZipOutputStream;
034    
035    /**
036     *
037     * @version $Rev: 706640 $ $Date: 2008-10-21 14:44:05 +0000 (Tue, 21 Oct 2008) $
038     */
039    public class ZipDirectoryPackager implements DirectoryPackager {
040    
041        public File pack(File configurationDir) throws IOException {
042            File zippedDir = File.createTempFile(configurationDir.getName(), ".zip");
043    
044            OutputStream out = new FileOutputStream(zippedDir);
045            out = new BufferedOutputStream(out);
046            ZipOutputStream zos = new ZipOutputStream(out);
047            zip(zos, configurationDir, configurationDir);
048            zos.close();
049    
050            return zippedDir;
051        }
052    
053        public File unpack(File packedConfigurationDir) throws IOException {
054            String tmpDirAsString = System.getProperty("java.io.tmpdir");
055            File targetDir = new File(new File(tmpDirAsString), packedConfigurationDir.getName() + "_unpack");
056            unpack(targetDir, packedConfigurationDir);
057            return targetDir;
058        }
059        
060        public void unpack(File targetDir, File packedConfigurationDir) throws IOException {
061            ZipFile zipFile = new ZipFile(packedConfigurationDir);
062            Enumeration<? extends ZipEntry> zipEntries = zipFile.entries();
063            while (zipEntries.hasMoreElements()) {
064                ZipEntry zipEntry = zipEntries.nextElement();
065                File targetFile = new File(targetDir, zipEntry.getName());
066    
067                if (zipEntry.isDirectory()) {
068                    targetFile.mkdirs();
069                } else {
070                    targetFile.getParentFile().mkdirs();
071                    targetFile.createNewFile();
072                    OutputStream out = new FileOutputStream(targetFile);
073                    out = new BufferedOutputStream(out);
074                    InputStream in = zipFile.getInputStream(zipEntry);
075    
076                    byte[] buffer = new byte[1024];
077                    int read;
078                    while (-1 != (read = in.read(buffer))) {
079                        out.write(buffer, 0, read);
080                    }
081                    
082                    in.close();
083                    out.close();
084                }
085            }
086        }
087        
088        protected void zip(ZipOutputStream zos, File configurationDir, File nestedFile) throws IOException {
089            if (nestedFile.isDirectory()) {
090                File[] nestedFiles = nestedFile.listFiles();
091                for (int i = 0; i < nestedFiles.length; i++) {
092                    zip(zos, configurationDir, nestedFiles[i]);
093                }
094            } else {
095                String nestedFilePath = nestedFile.getAbsolutePath();
096                String zipEntryName = nestedFilePath.substring(configurationDir.getAbsolutePath().length() + 1, nestedFilePath.length());
097                ZipEntry zipEntry = new ZipEntry(zipEntryName);
098                zos.putNextEntry(zipEntry);
099                
100                InputStream in = new FileInputStream(nestedFile);
101                in = new BufferedInputStream(in);
102                
103                byte[] buffer = new byte[1024];
104                int read;
105                while (-1 != (read = in.read(buffer))) {
106                    zos.write(buffer, 0, read);
107                }
108    
109                in.close();
110                zos.closeEntry();
111            }
112        }
113    
114    }