001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.tests.harness.util;
011:
012: import java.io.File;
013: import java.io.FileInputStream;
014: import java.io.FileOutputStream;
015: import java.io.FileReader;
016: import java.io.FileWriter;
017: import java.io.IOException;
018: import java.io.InputStream;
019: import java.io.OutputStream;
020: import java.io.Reader;
021: import java.io.Writer;
022: import java.net.URL;
023: import java.util.Enumeration;
024: import java.util.zip.ZipEntry;
025: import java.util.zip.ZipFile;
026:
027: import org.eclipse.core.runtime.IPath;
028: import org.eclipse.core.runtime.Platform;
029: import org.eclipse.core.runtime.Plugin;
030:
031: public class FileTool {
032:
033: /**
034: * A buffer.
035: */
036: private static byte[] buffer = new byte[8192];
037:
038: /**
039: * Unzips the given zip file to the given destination directory
040: * extracting only those entries the pass through the given
041: * filter.
042: *
043: * @param filter filters out unwanted zip entries
044: * @param zipFile the zip file to unzip
045: * @param dstDir the destination directory
046: */
047: public static void unzip(ZipFile zipFile, File dstDir)
048: throws IOException {
049: unzip(zipFile, dstDir, dstDir, 0);
050: }
051:
052: private static void unzip(ZipFile zipFile, File rootDstDir,
053: File dstDir, int depth) throws IOException {
054:
055: Enumeration entries = zipFile.entries();
056:
057: try {
058: while (entries.hasMoreElements()) {
059: ZipEntry entry = (ZipEntry) entries.nextElement();
060: if (entry.isDirectory()) {
061: continue;
062: }
063: String entryName = entry.getName();
064: File file = new File(dstDir, changeSeparator(entryName,
065: '/', File.separatorChar));
066: file.getParentFile().mkdirs();
067: InputStream src = null;
068: OutputStream dst = null;
069: try {
070: src = zipFile.getInputStream(entry);
071: dst = new FileOutputStream(file);
072: transferData(src, dst);
073: } finally {
074: if (dst != null) {
075: try {
076: dst.close();
077: } catch (IOException e) {
078: }
079: }
080: if (src != null) {
081: try {
082: src.close();
083: } catch (IOException e) {
084: }
085: }
086: }
087: }
088: } finally {
089: try {
090: zipFile.close();
091: } catch (IOException e) {
092: }
093: }
094: }
095:
096: /**
097: * Returns the given file path with its separator
098: * character changed from the given old separator to the
099: * given new separator.
100: *
101: * @param path a file path
102: * @param oldSeparator a path separator character
103: * @param newSeparator a path separator character
104: * @return the file path with its separator character
105: * changed from the given old separator to the given new
106: * separator
107: */
108: public static String changeSeparator(String path,
109: char oldSeparator, char newSeparator) {
110: return path.replace(oldSeparator, newSeparator);
111: }
112:
113: /**
114: * Copies all bytes in the given source file to
115: * the given destination file.
116: *
117: * @param source the given source file
118: * @param destination the given destination file
119: */
120: public static void transferData(File source, File destination)
121: throws IOException {
122: destination.getParentFile().mkdirs();
123: InputStream is = null;
124: OutputStream os = null;
125: try {
126: is = new FileInputStream(source);
127: os = new FileOutputStream(destination);
128: transferData(is, os);
129: } finally {
130: if (os != null) {
131: try {
132: os.close();
133: } catch (IOException e) {
134: }
135: }
136: if (is != null) {
137: try {
138: is.close();
139: } catch (IOException e) {
140: }
141: }
142: }
143: }
144:
145: /**
146: * Copies all bytes in the given source stream to
147: * the given destination stream. Neither streams
148: * are closed.
149: *
150: * @param source the given source stream
151: * @param destination the given destination stream
152: */
153: public static void transferData(InputStream source,
154: OutputStream destination) throws IOException {
155: int bytesRead = 0;
156: while (bytesRead != -1) {
157: bytesRead = source.read(buffer, 0, buffer.length);
158: if (bytesRead != -1) {
159: destination.write(buffer, 0, bytesRead);
160: }
161: }
162: }
163:
164: /**
165: * Copies the given source file to the given destination file.
166: *
167: * @param src the given source file
168: * @param dst the given destination file
169: */
170: public static void copy(File src, File dst) throws IOException {
171: if (src.isDirectory()) {
172: String[] srcChildren = src.list();
173: for (int i = 0; i < srcChildren.length; ++i) {
174: File srcChild = new File(src, srcChildren[i]);
175: File dstChild = new File(dst, srcChildren[i]);
176: copy(srcChild, dstChild);
177: }
178: } else
179: transferData(src, dst);
180: }
181:
182: public static File getFileInPlugin(Plugin plugin, IPath path) {
183: try {
184: URL installURL = plugin.getBundle().getEntry(
185: path.toString());
186: URL localURL = Platform.asLocalURL(installURL);
187: return new File(localURL.getFile());
188: } catch (IOException e) {
189: return null;
190: }
191: }
192:
193: public static StringBuffer read(String fileName) throws IOException {
194: return read(new FileReader(fileName));
195: }
196:
197: public static StringBuffer read(Reader reader) throws IOException {
198: StringBuffer s = new StringBuffer();
199: try {
200: char[] buffer = new char[8196];
201: int chars = reader.read(buffer);
202: while (chars != -1) {
203: s.append(buffer, 0, chars);
204: chars = reader.read(buffer);
205: }
206: } finally {
207: try {
208: reader.close();
209: } catch (IOException e) {
210: }
211: }
212: return s;
213: }
214:
215: public static void write(String fileName, StringBuffer content)
216: throws IOException {
217: Writer writer = new FileWriter(fileName);
218: try {
219: writer.write(content.toString());
220: } finally {
221: try {
222: writer.close();
223: } catch (IOException e) {
224: }
225: }
226: }
227: }
|