1 /******************************************************************************* 2 * Copyright (c) 2000, 2017 IBM Corporation and others. 3 * 4 * This program and the accompanying materials 5 * are made available under the terms of the Eclipse Public License 2.0 6 * which accompanies this distribution, and is available at 7 * https://www.eclipse.org/legal/epl-2.0/ 8 * 9 * SPDX-License-Identifier: EPL-2.0 10 * 11 * Contributors: 12 * IBM Corporation - initial API and implementation 13 * Jeanderson Candido <http://jeandersonbc.github.io> - Bug 444070 14 *******************************************************************************/ 15 16 package org.eclipse.ui.tests.harness.util; 17 18 import java.io.File; 19 import java.io.FileInputStream; 20 import java.io.FileOutputStream; 21 import java.io.FileReader; 22 import java.io.FileWriter; 23 import java.io.IOException; 24 import java.io.InputStream; 25 import java.io.OutputStream; 26 import java.io.Reader; 27 import java.io.Writer; 28 import java.net.URL; 29 import java.util.Enumeration; 30 import java.util.zip.ZipEntry; 31 import java.util.zip.ZipFile; 32 33 import org.eclipse.core.runtime.FileLocator; 34 import org.eclipse.core.runtime.IPath; 35 import org.eclipse.core.runtime.Plugin; 36 37 38 public class FileTool { 39 40 /** 41 * A buffer. 42 */ 43 private static byte[] buffer = new byte[8192]; 44 /** 45 * Unzips the given zip file to the given destination directory 46 * extracting only those entries the pass through the given 47 * filter. 48 * 49 * @param zipFile the zip file to unzip 50 * @param dstDir the destination directory 51 */ unzip(ZipFile zipFile, File dstDir)52 public static void unzip(ZipFile zipFile, File dstDir) throws IOException { 53 Enumeration<? extends ZipEntry> entries = zipFile.entries(); 54 try { 55 while(entries.hasMoreElements()){ 56 ZipEntry entry = entries.nextElement(); 57 if(entry.isDirectory()){ 58 continue; 59 } 60 String entryName = entry.getName(); 61 File file = new File(dstDir, changeSeparator(entryName, '/', File.separatorChar)); 62 file.getParentFile().mkdirs(); 63 try (InputStream src = zipFile.getInputStream(entry); OutputStream dst= new FileOutputStream(file)){ 64 transferData(src, dst); 65 } 66 } 67 } finally { 68 try { 69 zipFile.close(); 70 } catch(IOException e){ 71 } 72 } 73 } 74 /** 75 * Returns the given file path with its separator 76 * character changed from the given old separator to the 77 * given new separator. 78 * 79 * @param path a file path 80 * @param oldSeparator a path separator character 81 * @param newSeparator a path separator character 82 * @return the file path with its separator character 83 * changed from the given old separator to the given new 84 * separator 85 */ changeSeparator(String path, char oldSeparator, char newSeparator)86 public static String changeSeparator(String path, char oldSeparator, char newSeparator){ 87 return path.replace(oldSeparator, newSeparator); 88 } 89 /** 90 * Copies all bytes in the given source file to 91 * the given destination file. 92 * 93 * @param source the given source file 94 * @param destination the given destination file 95 */ transferData(File source, File destination)96 public static void transferData(File source, File destination) throws IOException { 97 destination.getParentFile().mkdirs(); 98 try (InputStream is = new FileInputStream(source); OutputStream os = new FileOutputStream(destination)) { 99 transferData(is, os); 100 } 101 } 102 /** 103 * Copies all bytes in the given source stream to 104 * the given destination stream. Neither streams 105 * are closed. 106 * 107 * @param source the given source stream 108 * @param destination the given destination stream 109 */ transferData(InputStream source, OutputStream destination)110 public static void transferData(InputStream source, OutputStream destination) throws IOException { 111 int bytesRead = 0; 112 while(bytesRead != -1){ 113 bytesRead = source.read(buffer, 0, buffer.length); 114 if(bytesRead != -1){ 115 destination.write(buffer, 0, bytesRead); 116 } 117 } 118 } 119 120 /** 121 * Copies the given source file to the given destination file. 122 * 123 * @param src the given source file 124 * @param dst the given destination file 125 */ copy(File src, File dst)126 public static void copy(File src, File dst) throws IOException { 127 if(src.isDirectory()){ 128 String[] srcChildren = src.list(); 129 if (srcChildren == null) { 130 throw new IOException("Content from directory '" + src.getAbsolutePath() + "' can not be listed."); //$NON-NLS-1$ //$NON-NLS-2$ 131 } 132 for (String srcChildPathName : srcChildren) { 133 File srcChild = new File(src, srcChildPathName); 134 File dstChild = new File(dst, srcChildPathName); 135 copy(srcChild, dstChild); 136 } 137 } else 138 transferData(src, dst); 139 } 140 getFileInPlugin(Plugin plugin, IPath path)141 public static File getFileInPlugin(Plugin plugin, IPath path) { 142 try { 143 URL installURL= plugin.getBundle().getEntry(path.toString()); 144 URL localURL = FileLocator.toFileURL(installURL); 145 return new File(localURL.getFile()); 146 } catch (IOException e) { 147 return null; 148 } 149 } 150 readToBuilder(String fileName)151 public static StringBuilder readToBuilder(String fileName) throws IOException { 152 try (FileReader reader = new FileReader(fileName)) { 153 return readToBuilder(reader); 154 } 155 } 156 readToBuilder(Reader reader)157 public static StringBuilder readToBuilder(Reader reader) throws IOException { 158 StringBuilder s = new StringBuilder(); 159 try { 160 char[] buffer = new char[8196]; 161 int chars = reader.read(buffer); 162 while (chars != -1) { 163 s.append(buffer, 0, chars); 164 chars = reader.read(buffer); 165 } 166 } finally { 167 try { 168 reader.close(); 169 } catch (IOException e) { 170 } 171 } 172 return s; 173 } 174 writeFromBuilder(String fileName, StringBuilder content)175 public static void writeFromBuilder(String fileName, StringBuilder content) throws IOException { 176 try (Writer writer = new FileWriter(fileName)) { 177 writer.write(content.toString()); 178 } 179 } 180 } 181