1 18 19 package org.apache.tools.zip; 20 21 import java.util.Hashtable ; 22 import java.util.Vector ; 23 import java.util.zip.ZipException ; 24 25 29 public class ExtraFieldUtils { 30 31 36 private static Hashtable implementations; 37 38 static { 39 implementations = new Hashtable (); 40 register(AsiExtraField.class); 41 register(JarMarker.class); 42 } 43 44 53 public static void register(Class c) { 54 try { 55 ZipExtraField ze = (ZipExtraField) c.newInstance(); 56 implementations.put(ze.getHeaderId(), c); 57 } catch (ClassCastException cc) { 58 throw new RuntimeException (c + " doesn\'t implement ZipExtraField"); 59 } catch (InstantiationException ie) { 60 throw new RuntimeException (c + " is not a concrete class"); 61 } catch (IllegalAccessException ie) { 62 throw new RuntimeException (c + "\'s no-arg constructor is not public"); 63 } 64 } 65 66 75 public static ZipExtraField createExtraField(ZipShort headerId) 76 throws InstantiationException , IllegalAccessException { 77 Class c = (Class ) implementations.get(headerId); 78 if (c != null) { 79 return (ZipExtraField) c.newInstance(); 80 } 81 UnrecognizedExtraField u = new UnrecognizedExtraField(); 82 u.setHeaderId(headerId); 83 return u; 84 } 85 86 94 public static ZipExtraField[] parse(byte[] data) throws ZipException { 95 Vector v = new Vector (); 96 int start = 0; 97 while (start <= data.length - 4) { 98 ZipShort headerId = new ZipShort(data, start); 99 int length = (new ZipShort(data, start + 2)).getValue(); 100 if (start + 4 + length > data.length) { 101 throw new ZipException ("data starting at " + start 102 + " is in unknown format"); 103 } 104 try { 105 ZipExtraField ze = createExtraField(headerId); 106 ze.parseFromLocalFileData(data, start + 4, length); 107 v.addElement(ze); 108 } catch (InstantiationException ie) { 109 throw new ZipException (ie.getMessage()); 110 } catch (IllegalAccessException iae) { 111 throw new ZipException (iae.getMessage()); 112 } 113 start += (length + 4); 114 } 115 if (start != data.length) { throw new ZipException ("data starting at " + start 117 + " is in unknown format"); 118 } 119 120 ZipExtraField[] result = new ZipExtraField[v.size()]; 121 v.copyInto(result); 122 return result; 123 } 124 125 131 public static byte[] mergeLocalFileDataData(ZipExtraField[] data) { 132 int sum = 4 * data.length; 133 for (int i = 0; i < data.length; i++) { 134 sum += data[i].getLocalFileDataLength().getValue(); 135 } 136 byte[] result = new byte[sum]; 137 int start = 0; 138 for (int i = 0; i < data.length; i++) { 139 System.arraycopy(data[i].getHeaderId().getBytes(), 140 0, result, start, 2); 141 System.arraycopy(data[i].getLocalFileDataLength().getBytes(), 142 0, result, start + 2, 2); 143 byte[] local = data[i].getLocalFileDataData(); 144 System.arraycopy(local, 0, result, start + 4, local.length); 145 start += (local.length + 4); 146 } 147 return result; 148 } 149 150 156 public static byte[] mergeCentralDirectoryData(ZipExtraField[] data) { 157 int sum = 4 * data.length; 158 for (int i = 0; i < data.length; i++) { 159 sum += data[i].getCentralDirectoryLength().getValue(); 160 } 161 byte[] result = new byte[sum]; 162 int start = 0; 163 for (int i = 0; i < data.length; i++) { 164 System.arraycopy(data[i].getHeaderId().getBytes(), 165 0, result, start, 2); 166 System.arraycopy(data[i].getCentralDirectoryLength().getBytes(), 167 0, result, start + 2, 2); 168 byte[] local = data[i].getCentralDirectoryData(); 169 System.arraycopy(local, 0, result, start + 4, local.length); 170 start += (local.length + 4); 171 } 172 return result; 173 } 174 } 175 | Popular Tags |