Mycat 类图研究以及源码阅读之JarLoader
Mycat 源码阅读之JarLoader
package io.mycat.config.classloader;
import java.util.jar.*;
import java.lang.reflect.*;
import java.net.URL;
import java.net.URLClassLoader;
import java.io.*;
import java.util.*;
public class JarLoader {
/** Unpack a jar file into a directory. */
public static void unJar(File jarFile, File toDir) throws IOException {
JarFile jar = new JarFile(jarFile);
try {
Enumeration entries = jar.entries();
while (entries.hasMoreElements()) {
JarEntry entry = (JarEntry)entries.nextElement();
if (!entry.isDirectory()) {
InputStream in = jar.getInputStream(entry);
try {
File file = new File(toDir, entry.getName());
if (!file.getParentFile().mkdirs() && !file.getParentFile().isDirectory()) {
throw new IOException("Mkdirs failed to create " +
file.getParentFile().toString());
}
OutputStream out = new FileOutputStream(file);
try {
byte[] buffer = new byte[8192];
int i;
while ((i = in.read(buffer)) != -1) {
out.write(buffer, 0, i);
}
} finally {
out.close();
}
} finally {
in.close();
}
}
}
} finally {
jar.close();
}
}
}
测试代码:
public class JarDemo {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
File jarFile = new File("C:/Intel/joda-time-2.3.jar");
File toDir = new File("C:/Intel");
JarLoader.unJar(jarFile, toDir);
}
}
解压之后的文件夹
Mycat类图研究