1 17 18 package net.sourceforge.groboutils.codecoverage.v2.ant.zip; 19 20 import java.util.Hashtable ; 21 import java.util.Vector ; 22 import java.util.zip.ZipException ; 23 24 30 public class ExtraFieldUtils { 31 32 37 private static Hashtable implementations; 38 39 static { 40 implementations = new Hashtable (); 41 register(AsiExtraField.class); 42 } 43 44 52 public static void register(Class c) { 53 try { 54 ZipExtraField ze = (ZipExtraField) c.newInstance(); 55 implementations.put(ze.getHeaderId(), c); 56 } catch (ClassCastException cc) { 57 throw new RuntimeException (c + " doesn\'t implement ZipExtraField"); 58 } catch (InstantiationException ie) { 59 throw new RuntimeException (c + " is not a concrete class"); 60 } catch (IllegalAccessException ie) { 61 throw new RuntimeException (c + "\'s no-arg constructor is not public"); 62 } 63 } 64 65 71 public static ZipExtraField createExtraField(ZipShort headerId) 72 throws InstantiationException , IllegalAccessException { 73 Class c = (Class ) implementations.get(headerId); 74 if (c != null) { 75 return (ZipExtraField) c.newInstance(); 76 } 77 UnrecognizedExtraField u = new UnrecognizedExtraField(); 78 u.setHeaderId(headerId); 79 return u; 80 } 81 82 88 public static ZipExtraField[] parse(byte[] data) throws ZipException { 89 Vector v = new Vector (); 90 int start = 0; 91 while (start <= data.length - 4) { 92 ZipShort headerId = new ZipShort(data, start); 93 int length = (new ZipShort(data, start + 2)).getValue(); 94 if (start + 4 + length > data.length) { 95 throw new ZipException ("data starting at " + start 96 + " is in unknown format"); 97 } 98 try { 99 ZipExtraField ze = createExtraField(headerId); 100 ze.parseFromLocalFileData(data, start + 4, length); 101 v.addElement(ze); 102 } catch (InstantiationException ie) { 103 throw new ZipException (ie.getMessage()); 104 } catch (IllegalAccessException iae) { 105 throw new ZipException (iae.getMessage()); 106 } 107 start += (length + 4); 108 } 109 if (start != data.length) { throw new ZipException ("data starting at " + start 111 + " is in unknown format"); 112 } 113 114 ZipExtraField[] result = new ZipExtraField[v.size()]; 115 v.copyInto(result); 116 return result; 117 } 118 119 124 public static byte[] mergeLocalFileDataData(ZipExtraField[] data) { 125 int sum = 4 * data.length; 126 for (int i = 0; i < data.length; i++) { 127 sum += data[i].getLocalFileDataLength().getValue(); 128 } 129 byte[] result = new byte[sum]; 130 int start = 0; 131 for (int i = 0; i < data.length; i++) { 132 System.arraycopy(data[i].getHeaderId().getBytes(), 133 0, result, start, 2); 134 System.arraycopy(data[i].getLocalFileDataLength().getBytes(), 135 0, result, start + 2, 2); 136 byte[] local = data[i].getLocalFileDataData(); 137 System.arraycopy(local, 0, result, start + 4, local.length); 138 start += (local.length + 4); 139 } 140 return result; 141 } 142 143 148 public static byte[] mergeCentralDirectoryData(ZipExtraField[] data) { 149 int sum = 4 * data.length; 150 for (int i = 0; i < data.length; i++) { 151 sum += data[i].getCentralDirectoryLength().getValue(); 152 } 153 byte[] result = new byte[sum]; 154 int start = 0; 155 for (int i = 0; i < data.length; i++) { 156 System.arraycopy(data[i].getHeaderId().getBytes(), 157 0, result, start, 2); 158 System.arraycopy(data[i].getCentralDirectoryLength().getBytes(), 159 0, result, start + 2, 2); 160 byte[] local = data[i].getCentralDirectoryData(); 161 System.arraycopy(local, 0, result, start + 4, local.length); 162 start += (local.length + 4); 163 } 164 return result; 165 } 166 } 167 | Popular Tags |