
org.cryptable.zap.mavenplugin.Utils Maven / Gradle / Ivy
package org.cryptable.zap.mavenplugin;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.JarURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Enumeration;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import org.apache.commons.lang.StringUtils;
public class Utils {
static public String readFile(String path, Charset encoding) throws IOException {
byte[] encoded = Files.readAllBytes(Paths.get(path));
return new String(encoded, encoding);
}
private static boolean copyStream(final InputStream is, final File f) {
try {
System.out.println("Out to file: " + f.getAbsolutePath());
return Utils.copyStream(is, new FileOutputStream(f));
} catch (final FileNotFoundException e) {
e.printStackTrace();
}
return false;
}
private static boolean copyStream(final InputStream is, final OutputStream os) {
try {
final byte[] buf = new byte[1024];
int len;
while ((len = is.read(buf)) > 0) {
os.write(buf, 0, len);
}
is.close();
os.close();
return true;
} catch (final IOException e) {
e.printStackTrace();
}
return false;
}
private static boolean ensureDirectoryExists(final File f) {
return f.exists() || f.mkdir();
}
public static boolean copyFile(final File toCopy, final File destFile) {
try {
return Utils.copyStream(new FileInputStream(toCopy), new FileOutputStream(destFile));
} catch (final FileNotFoundException e) {
e.printStackTrace();
}
return false;
}
private static boolean copyFilesRecusively(final File toCopy, final File destDir) {
assert destDir.isDirectory();
if (!toCopy.isDirectory()) {
return Utils.copyFile(toCopy, new File(destDir, toCopy.getName()));
} else {
final File newDestDir = new File(destDir, toCopy.getName());
if (!newDestDir.exists() && !newDestDir.mkdir()) {
return false;
}
for (final File child : toCopy.listFiles()) {
if (!Utils.copyFilesRecusively(child, newDestDir)) {
return false;
}
}
}
return true;
}
public static boolean copyJarResourcesRecursively(final File destDir, final JarURLConnection jarConnection) throws IOException {
final JarFile jarFile = jarConnection.getJarFile();
for (final Enumeration e = jarFile.entries(); e.hasMoreElements();) {
final JarEntry entry = e.nextElement();
if (entry.getName().startsWith(jarConnection.getEntryName())) {
System.out.println("Processing JAR : " + entry.toString());
final String filename = StringUtils.removeStart(entry.getName(), jarConnection.getEntryName());
final File f = new File(destDir, filename);
if (!entry.isDirectory()) {
final InputStream entryInputStream = jarFile.getInputStream(entry);
if (!Utils.copyStream(entryInputStream, f)) {
return false;
}
entryInputStream.close();
} else {
if (!Utils.ensureDirectoryExists(f)) {
throw new IOException("Could not create directory: " + f.getAbsolutePath());
}
}
}
}
return true;
}
public static boolean copyResourcesRecursively(final URL originUrl, final File destination) {
try {
final URLConnection urlConnection = originUrl.openConnection();
if (urlConnection instanceof JarURLConnection) {
System.out.println("Processing JAR : " + originUrl.toString());
return Utils.copyJarResourcesRecursively(destination, (JarURLConnection) urlConnection);
} else {
System.out.println("Processing File : " + originUrl.toString());
return Utils.copyFilesRecusively(new File(originUrl.getPath()), destination);
}
} catch (final IOException e) {
e.printStackTrace();
}
return false;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy