一、前言
关于grizzly-webserver源码中com.sun.grizzly.util.ExpandJar对于jar包处理类,进行对jar包文件的读取、删除、复制及目录删除等操作。
二、源码说明
package com.sun.grizzly.util;@b@@b@import java.io.BufferedOutputStream;@b@import java.io.File;@b@import java.io.FileInputStream;@b@import java.io.FileOutputStream;@b@import java.io.IOException;@b@import java.io.InputStream;@b@import java.net.JarURLConnection;@b@import java.net.URL;@b@import java.nio.channels.FileChannel;@b@import java.util.Enumeration;@b@import java.util.jar.JarEntry;@b@import java.util.jar.JarFile;@b@@b@public class ExpandJar@b@{@b@ public static String expand(URL jar)@b@ throws IOException@b@ {@b@ String pathname = jar.toString().replace('\\', '/');@b@ if (pathname.endsWith("!/"))@b@ pathname = pathname.substring(0, pathname.length() - 2);@b@@b@ int period = pathname.lastIndexOf(46);@b@ if (period >= pathname.length() - 4)@b@ pathname = pathname.substring(0, period);@b@ int slash = pathname.lastIndexOf(47);@b@ if (slash >= 0)@b@ pathname = pathname.substring(slash + 1);@b@@b@ return expand(jar, pathname, System.getProperty("java.io.tmpdir"));@b@ }@b@@b@ public static String expand(URL jar, String pathname, String dirname)@b@ throws IOException@b@ {@b@ File appBase = new File(dirname);@b@ File docBase = new File(appBase, pathname);@b@@b@ docBase.mkdir();@b@@b@ JarURLConnection juc = (JarURLConnection)jar.openConnection();@b@ juc.setUseCaches(false);@b@ JarFile jarFile = null;@b@ InputStream input = null;@b@ try {@b@ jarFile = juc.getJarFile();@b@ Enumeration jarEntries = jarFile.entries();@b@ while (true) { JarEntry jarEntry;@b@ String name;@b@ while (true) { if (!(jarEntries.hasMoreElements())) break label171;@b@ jarEntry = (JarEntry)jarEntries.nextElement();@b@ name = jarEntry.getName();@b@ int last = name.lastIndexOf(47);@b@ if (last >= 0) {@b@ File parent = new File(docBase, name.substring(0, last));@b@@b@ parent.mkdirs();@b@ }@b@ if (!(name.endsWith("/")))@b@ break;@b@ }@b@ input = jarFile.getInputStream(jarEntry);@b@ expand(input, docBase, name);@b@ input.close();@b@ label171: input = null;@b@ }@b@ }@b@ catch (IOException e)@b@ {@b@ }@b@ finally@b@ {@b@ if (input != null) {@b@ try {@b@ input.close();@b@ }@b@ catch (Throwable t) {@b@ }@b@ input = null;@b@ }@b@ if (jarFile != null) {@b@ try {@b@ jarFile.close();@b@ }@b@ catch (Throwable t) {@b@ }@b@ jarFile = null;@b@ }@b@@b@ }@b@@b@ return docBase.getAbsolutePath();@b@ }@b@@b@ public static boolean copy(File src, File dest)@b@ {@b@ boolean result = true;@b@@b@ String[] files = null;@b@ if (src.isDirectory()) {@b@ files = src.list();@b@ result = dest.mkdir();@b@ } else {@b@ files = new String[1];@b@ files[0] = "";@b@ }@b@ if (files == null)@b@ files = new String[0];@b@@b@ for (int i = 0; (i < files.length) && (result); ++i) {@b@ File fileSrc = new File(src, files[i]);@b@ File fileDest = new File(dest, files[i]);@b@ if (fileSrc.isDirectory()) {@b@ result = copy(fileSrc, fileDest);@b@ } else {@b@ FileChannel ic = null;@b@ FileChannel oc = null;@b@ try {@b@ ic = new FileInputStream(fileSrc).getChannel();@b@ oc = new FileOutputStream(fileDest).getChannel();@b@ ic.transferTo(0L, ic.size(), oc);@b@ }@b@ catch (IOException e) {@b@ result = false;@b@ } finally {@b@ if (ic != null)@b@ try {@b@ ic.close();@b@ }@b@ catch (IOException e) {@b@ }@b@ if (oc != null)@b@ try {@b@ oc.close();@b@ }@b@ catch (IOException e) {@b@ }@b@ }@b@ }@b@ }@b@ return result;@b@ }@b@@b@ public static boolean delete(File dir)@b@ {@b@ if (dir.isDirectory())@b@ return deleteDir(dir);@b@@b@ return dir.delete();@b@ }@b@@b@ public static boolean deleteDir(File dir)@b@ {@b@ String[] files = dir.list();@b@ if (files == null)@b@ files = new String[0];@b@@b@ for (int i = 0; i < files.length; ++i) {@b@ File file = new File(dir, files[i]);@b@ if (file.isDirectory())@b@ deleteDir(file);@b@ else@b@ file.delete();@b@ }@b@@b@ return dir.delete();@b@ }@b@@b@ protected static void expand(InputStream input, File docBase, String name)@b@ throws IOException@b@ {@b@ File file = new File(docBase, name);@b@ BufferedOutputStream output = null;@b@ try {@b@ output = new BufferedOutputStream(new FileOutputStream(file));@b@@b@ byte[] buffer = new byte[2048];@b@ while (true) {@b@ int n = input.read(buffer);@b@ if (n <= 0)@b@ break;@b@ output.write(buffer, 0, n);@b@ }@b@ } finally {@b@ if (output != null)@b@ try {@b@ output.close();@b@ }@b@ catch (IOException e)@b@ {@b@ }@b@ }@b@ }@b@}