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:$ $Date:$
038: */
039: public class ZipDirectoryPackager implements DirectoryPackager {
040:
041: public File pack(File configurationDir) throws IOException {
042: File zippedDir = File.createTempFile(
043: configurationDir.getName(), ".zip");
044:
045: OutputStream out = new FileOutputStream(zippedDir);
046: out = new BufferedOutputStream(out);
047: ZipOutputStream zos = new ZipOutputStream(out);
048: zip(zos, configurationDir, configurationDir);
049: zos.close();
050:
051: return zippedDir;
052: }
053:
054: public File unpack(File packedConfigurationDir) throws IOException {
055: String tmpDirAsString = System.getProperty("java.io.tmpdir");
056: File targetDir = new File(new File(tmpDirAsString),
057: packedConfigurationDir.getName() + "_unpack");
058: unpack(targetDir, packedConfigurationDir);
059: return targetDir;
060: }
061:
062: public void unpack(File targetDir, File packedConfigurationDir)
063: throws IOException {
064: ZipFile zipFile = new ZipFile(packedConfigurationDir);
065: Enumeration<? extends ZipEntry> zipEntries = zipFile.entries();
066: while (zipEntries.hasMoreElements()) {
067: ZipEntry zipEntry = zipEntries.nextElement();
068: File targetFile = new File(targetDir, zipEntry.getName());
069:
070: if (zipEntry.isDirectory()) {
071: targetFile.mkdirs();
072: } else {
073: targetFile.getParentFile().mkdirs();
074: targetFile.createNewFile();
075: OutputStream out = new FileOutputStream(targetFile);
076: out = new BufferedOutputStream(out);
077: InputStream in = zipFile.getInputStream(zipEntry);
078:
079: byte[] buffer = new byte[1024];
080: int read;
081: while (-1 != (read = in.read(buffer))) {
082: out.write(buffer, 0, read);
083: }
084:
085: in.close();
086: out.close();
087: }
088: }
089: }
090:
091: protected void zip(ZipOutputStream zos, File configurationDir,
092: File nestedFile) throws IOException {
093: if (nestedFile.isDirectory()) {
094: File[] nestedFiles = nestedFile.listFiles();
095: for (int i = 0; i < nestedFiles.length; i++) {
096: zip(zos, configurationDir, nestedFiles[i]);
097: }
098: } else {
099: String nestedFilePath = nestedFile.getAbsolutePath();
100: String zipEntryName = nestedFilePath.substring(
101: configurationDir.getAbsolutePath().length() + 1,
102: nestedFilePath.length());
103: ZipEntry zipEntry = new ZipEntry(zipEntryName);
104: zos.putNextEntry(zipEntry);
105:
106: InputStream in = new FileInputStream(nestedFile);
107: in = new BufferedInputStream(in);
108:
109: byte[] buffer = new byte[1024];
110: int read;
111: while (-1 != (read = in.read(buffer))) {
112: zos.write(buffer, 0, read);
113: }
114:
115: in.close();
116: zos.closeEntry();
117: }
118: }
119:
120: }
|