1 32 33 package com.jeantessier.classreader; 34 35 import java.io.*; 36 import java.util.*; 37 import java.util.zip.*; 38 39 import org.apache.log4j.*; 40 41 public class ZipClassfileLoader extends ClassfileLoaderDecorator { 42 private static final int BUFFER_SIZE = 16 * 1024; 43 44 public ZipClassfileLoader(ClassfileLoader loader) { 45 super(loader); 46 } 47 48 protected void load(String filename) { 49 Logger.getLogger(getClass()).debug("Starting group in file " + filename); 50 51 ZipFile zipfile = null; 52 try { 53 zipfile = new ZipFile(filename); 54 55 fireBeginGroup(filename, zipfile.size()); 56 57 Logger.getLogger(getClass()).debug("Loading ZipFile " + filename); 58 load(zipfile); 59 Logger.getLogger(getClass()).debug("Loaded ZipFile " + filename); 60 61 fireEndGroup(filename); 62 } catch (IOException ex) { 63 Logger.getLogger(getClass()).error("Cannot load Zip file \"" + filename + "\"", ex); 64 } finally { 65 if (zipfile != null) { 66 try { 67 zipfile.close(); 68 } catch (IOException ex) { 69 } 71 } 72 } 73 } 74 75 protected void load(String filename, InputStream in) { 76 Logger.getLogger(getClass()).debug("Starting group in stream " + filename); 77 78 ZipInputStream zipfile = null; 79 try { 80 zipfile = new ZipInputStream(in); 81 82 fireBeginGroup(filename, -1); 83 84 Logger.getLogger(getClass()).debug("Loading ZipInputStream " + filename); 85 load(zipfile); 86 Logger.getLogger(getClass()).debug("Loaded ZipInputStream " + filename); 87 88 fireEndGroup(filename); 89 } catch (IOException ex) { 90 Logger.getLogger(getClass()).error("Cannot load Zip file \"" + filename + "\"", ex); 91 } finally { 92 if (zipfile != null) { 93 try { 94 zipfile.close(); 95 } catch (IOException ex) { 96 } 98 } 99 } 100 } 101 102 protected void load(ZipFile zipfile) throws IOException { 103 Enumeration entries = zipfile.entries(); 104 while(entries.hasMoreElements()) { 105 ZipEntry entry = (ZipEntry) entries.nextElement(); 106 107 fireBeginFile(entry.getName()); 108 109 Logger.getLogger(getClass()).debug("Starting file " + entry.getName() + " (" + entry.getSize() + " bytes)"); 110 111 byte[] bytes = null; 112 InputStream in = null; 113 try { 114 in = zipfile.getInputStream(entry); 115 bytes = readBytes(in); 116 } finally { 117 if (in != null) { 118 try { 119 in.close(); 120 } catch (IOException ex) { 121 } 123 } 124 } 125 126 Logger.getLogger(getClass()).debug("Passing up file " + entry.getName() + " (" + bytes.length + " bytes)"); 127 getLoader().load(entry.getName(), new ByteArrayInputStream(bytes)); 128 129 fireEndFile(entry.getName()); 130 } 131 } 132 133 protected void load(ZipInputStream in) throws IOException { 134 ZipEntry entry; 135 while ((entry = in.getNextEntry()) != null) { 136 fireBeginFile(entry.getName()); 137 138 Logger.getLogger(getClass()).debug("Starting file " + entry.getName() + " (" + entry.getSize() + " bytes)"); 139 byte[] bytes = readBytes(in); 140 141 Logger.getLogger(getClass()).debug("Passing up file " + entry.getName() + " (" + bytes.length + " bytes)"); 142 getLoader().load(entry.getName(), new ByteArrayInputStream(bytes)); 143 144 fireEndFile(entry.getName()); 145 } 146 } 147 148 private byte[] readBytes(InputStream in) { 149 byte[] result = null; 150 151 try { 152 ByteArrayOutputStream out = new ByteArrayOutputStream(); 153 byte[] buffer = new byte[BUFFER_SIZE]; 154 int bytesRead = 0; 155 while ((bytesRead = in.read(buffer, 0, BUFFER_SIZE)) != -1) { 156 out.write(buffer, 0, bytesRead); 157 } 158 out.close(); 159 160 result = out.toByteArray(); 161 } catch (IOException ex) { 162 Logger.getLogger(getClass()).debug("Error loading Zip entry", ex); 163 } 164 165 return(result); 166 } 167 } 168 | Popular Tags |