001: /**
002: * $Id: PortalFileHandler.java,v 1.4 2007/01/26 03:48:33 portalbld Exp $
003: * Copyright 2004 Sun Microsystems, Inc. All
004: * rights reserved. Use of this product is subject
005: * to license terms. Federal Acquisitions:
006: * Commercial Software -- Government Users
007: * Subject to Standard License Terms and
008: * Conditions.
009: *
010: * Sun, Sun Microsystems, the Sun logo, and Sun ONE
011: * are trademarks or registered trademarks of Sun Microsystems,
012: * Inc. in the United States and other countries.
013: */package com.sun.portal.fabric.tasks;
014:
015: import java.io.File;
016: import java.io.FileInputStream;
017: import java.io.FileOutputStream;
018: import java.io.FileReader;
019: import java.io.FileNotFoundException;
020: import java.io.InputStream;
021: import java.io.InputStreamReader;
022: import java.io.IOException;
023:
024: import java.util.logging.Level;
025: import java.util.logging.Logger;
026: import java.util.Enumeration;
027: import java.util.ArrayList;
028: import java.util.Iterator;
029: import java.util.List;
030: import java.util.zip.ZipEntry;
031: import java.util.zip.ZipFile;
032: import java.util.zip.ZipOutputStream;
033: import com.sun.portal.fabric.util.FileUtil;
034: import com.sun.portal.log.common.PortalLogger;
035: import com.sun.portal.util.Platform;
036:
037: /**
038: * This is a utility class that creates a zip file out of the portal
039: * resources on the filesystem. This zipped file can be unzipped to setup the
040: * directory structure for a portal on a new host.
041: */
042: public class PortalFileHandler {
043:
044: private static Logger logger = PortalLogger
045: .getLogger(PortalFileHandler.class);
046:
047: public static void zipFilesystem(String portalDir, String output) {
048:
049: if (!portalDir.endsWith(Platform.fs)) {
050: portalDir = portalDir + Platform.fs;
051: }
052:
053: File outputFile = new File(output);
054: List fileList = new ArrayList(100);
055: getFiles(new File(portalDir), fileList);
056:
057: try {
058: // If a file with the output filename exists, delete it
059: if (outputFile.exists()) {
060: outputFile.delete();
061: }
062:
063: // Define the file read buffer size
064: byte[] buf = new byte[1024];
065:
066: // Create the ZIP file
067: FileOutputStream dest = new FileOutputStream(output);
068: ZipOutputStream out = new ZipOutputStream(dest);
069:
070: try {
071:
072: // Compress the files
073: Iterator itr = fileList.iterator();
074: while (itr.hasNext()) {
075:
076: File file = (File) itr.next();
077: String fpath = file.getAbsolutePath();
078: // Open input stream to transfer bytes from the file to the ZIP file
079: FileInputStream in = new FileInputStream(fpath);
080: String zipEntryPath = fpath.replaceAll(portalDir,
081: "");
082: try {
083: // Add ZIP entry to output stream.
084: out.putNextEntry(new ZipEntry(zipEntryPath));
085: int len;
086: while ((len = in.read(buf)) > 0) {
087: out.write(buf, 0, len);
088: }
089: } finally {
090: // Complete the entry
091: out.closeEntry();
092: in.close();
093: }
094: }
095:
096: } finally {
097: // Complete the ZIP file
098: out.close();
099: }
100:
101: } catch (Exception e) {
102: logger.log(Level.SEVERE, "PSFB_CSPFT0281", e);
103: // Discard the uncomplete file
104: if (outputFile.exists()) {
105: outputFile.delete();
106: }
107: }
108: }
109:
110: public static void unzipFilesystem(String portalDir,
111: String portalZip) {
112:
113: if (!portalDir.endsWith(Platform.fs)) {
114: portalDir = portalDir + Platform.fs;
115: }
116:
117: try {
118: // Open the zip.
119: ZipFile zip = new ZipFile(portalZip);
120: // Get entries in this zip file
121: Enumeration en = zip.entries();
122: try {
123: while (en.hasMoreElements()) {
124: // Get the entry
125: ZipEntry entry = (ZipEntry) en.nextElement();
126: // If the entry is not null, extract it.
127: if (entry != null) {
128: // Get an input stream for the entry.
129: InputStream entryStream = zip
130: .getInputStream(entry);
131: try {
132: // Create the output file
133: File outFile = new File(portalDir
134: + entry.getName());
135: File parent = outFile.getParentFile();
136: if (!parent.exists()) {
137: parent.mkdirs();
138: }
139: FileOutputStream file = new FileOutputStream(
140: outFile);
141: try {
142: // Allocate a buffer for reading the entry data.
143: byte[] buffer = new byte[1024];
144: int bytesRead;
145: // Read the entry data and write it to the output file.
146: while ((bytesRead = entryStream
147: .read(buffer)) != -1) {
148: file.write(buffer, 0, bytesRead);
149: }
150: } finally {
151: file.close();
152: }
153: } finally {
154: entryStream.close();
155: }
156: }
157: }
158: } finally {
159: zip.close();
160: }
161: } catch (Exception e) {
162: logger.log(Level.SEVERE, "PSFB_CSPFT0282", e);
163: }
164: }
165:
166: /**
167: * Builds a list of Files (absolute path) by recursively entering the dir.
168: */
169: private static void getFiles(File startDir, List fileList) {
170: File[] files = startDir.listFiles();
171: if (files != null) {
172: for (int i = 0; i < files.length; i++) {
173: File file = (File) files[i];
174: if (file.isDirectory()) {
175: getFiles(file, fileList);
176: } else {
177: fileList.add(file);
178: }
179: }
180: }
181: }
182:
183: }
|