Пример #1
0
 /**
  * Removes unparseable extra field data.
  *
  * @since Apache Commons Compress 1.1
  */
 public void removeUnparseableExtraFieldData()
 {
     if (unparseableExtra == null)
     {
         throw new java.util.NoSuchElementException();
     }
     unparseableExtra = null;
     setExtra();
 }
Пример #2
0
 /**
  * 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();
 }
Пример #3
0
 /**
  * Replaces all currently attached extra fields with the new array.
  * @param fields an array of extra fields
  */
 public void setExtraFields(ZipExtraField[] fields)
 {
     extraFields = new java.util.LinkedHashMap <ZipShort, ZipExtraField>();
     for (int i = 0; i < fields.Length; i++)
     {
         if (fields[i] is UnparseableExtraFieldData)
         {
             unparseableExtra = (UnparseableExtraFieldData)fields[i];
         }
         else
         {
             extraFields.put(fields[i].getHeaderId(), fields[i]);
         }
     }
     setExtra();
 }
Пример #4
0
 /**
  * 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();
 }
Пример #5
0
        /**
         * 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));
        }