/** * Register a ZipExtraField implementation. * * <p>The given class must have a no-arg constructor and implement * the {@link ZipExtraField ZipExtraField interface}.</p> * @param c the class to register */ public static void register(Type c) { try { ZipExtraField ze = (ZipExtraField)Activator.CreateInstance(c); implementations.put(ze.getHeaderId(), c); } catch (InvalidCastException) { throw new java.lang.RuntimeException(c + " doesn\'t implement ZipExtraField"); } catch (MethodAccessException) { throw new java.lang.RuntimeException(c + "\'s no-arg constructor is not public"); } catch (MemberAccessException) { throw new java.lang.RuntimeException(c + " is not a concrete class"); } }
/** * Adds an extra field - replacing an already present extra field * of the same type. * * <p />If no extra field of the same type exists, the field will be * added as last field. * @param ze an extra field */ public void addExtraField(ZipExtraField ze) { if (ze is UnparseableExtraFieldData) { unparseableExtra = (UnparseableExtraFieldData)ze; } else { if (extraFields == null) { extraFields = new java.util.LinkedHashMap <ZipShort, ZipExtraField>(); } extraFields.put(ze.getHeaderId(), ze); } setExtra(); }
/** * Adds an extra field - replacing an already present extra field * of the same type. * * <p />The new extra field will be the first one. * @param ze an extra field */ public void addAsFirstExtraField(ZipExtraField ze) { if (ze is UnparseableExtraFieldData) { unparseableExtra = (UnparseableExtraFieldData)ze; } else { java.util.LinkedHashMap <ZipShort, ZipExtraField> copy = extraFields; extraFields = new java.util.LinkedHashMap <ZipShort, ZipExtraField>(); extraFields.put(ze.getHeaderId(), ze); if (copy != null) { copy.remove(ze.getHeaderId()); extraFields.putAll(copy); } } setExtra(); }
/** * Split the array into ExtraFields and populate them with the * given data. * @param data an array of bytes * @param local whether data originates from the local file data * or the central directory * @param onUnparseableData what to do if the extra field data * cannot be parsed. * @return an array of ExtraFields * @throws ZipException on error * * @since Apache Commons Compress 1.1 */ public static ZipExtraField[] parse(byte[] data, bool local, UnparseableExtraField onUnparseableData) //throws ZipException { java.util.List <ZipExtraField> v = new java.util.ArrayList <ZipExtraField>(); int start = 0; LOOP: while (start <= data.Length - WORD) { ZipShort headerId = new ZipShort(data, start); int length = (new ZipShort(data, start + 2)).getValue(); if (start + WORD + length > data.Length) { switch (onUnparseableData.getKey()) { case UnparseableExtraField.THROW_KEY: throw new java.util.zip.ZipException("bad extra field starting at " + start + ". Block length of " + length + " bytes exceeds remaining" + " data of " + (data.Length - start - WORD) + " bytes."); case UnparseableExtraField.READ_KEY: UnparseableExtraFieldData field = new UnparseableExtraFieldData(); if (local) { field.parseFromLocalFileData(data, start, data.Length - start); } else { field.parseFromCentralDirectoryData(data, start, data.Length - start); } v.add(field); #region case UnparseableExtraField.SKIP_KEY: goto LOOP; #endregion //$FALL-THROUGH$ case UnparseableExtraField.SKIP_KEY: // since we cannot parse the data we must assume // the extra field consumes the whole rest of the // available data goto LOOP; // Basties Note: Bad coder!!! default: throw new java.util.zip.ZipException("unknown UnparseableExtraField key: " + onUnparseableData.getKey()); } } try { ZipExtraField ze = createExtraField(headerId); if (local) { ze.parseFromLocalFileData(data, start + WORD, length); } else { ze.parseFromCentralDirectoryData(data, start + WORD, length); } v.add(ze); } catch (MethodAccessException iae) { throw new java.util.zip.ZipException(iae.getMessage()); } catch (MemberAccessException ie) { throw new java.util.zip.ZipException(ie.getMessage()); } start += (length + WORD); } ZipExtraField[] result = new ZipExtraField[v.size()]; return((ZipExtraField[])v.toArray(result)); }