You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

UnparseableExtraFieldData.java 3.7 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. package org.apache.tools.zip;
  20. /**
  21. * Wrapper for extra field data that doesn't conform to the recommended format of header-tag + size + data.
  22. *
  23. * <p>The header-id is artificial (and not listed as a known ID in
  24. * <a href="http://www.pkware.com/documents/casestudies/APPNOTE.TXT">APPNOTE.TXT</a>).
  25. * Since it isn't used anywhere except to satisfy the
  26. * ZipExtraField contract it shouldn't matter anyway.</p>
  27. *
  28. * @since Ant 1.8.1
  29. */
  30. public final class UnparseableExtraFieldData
  31. implements CentralDirectoryParsingZipExtraField {
  32. private static final ZipShort HEADER_ID = new ZipShort(0xACC1);
  33. private byte[] localFileData;
  34. private byte[] centralDirectoryData;
  35. /**
  36. * The Header-ID.
  37. *
  38. * @return a completely arbitrary value that should be ignored.
  39. */
  40. public ZipShort getHeaderId() {
  41. return HEADER_ID;
  42. }
  43. /**
  44. * Length of the complete extra field in the local file data.
  45. *
  46. * @return The LocalFileDataLength value
  47. */
  48. public ZipShort getLocalFileDataLength() {
  49. return new ZipShort(localFileData == null ? 0 : localFileData.length);
  50. }
  51. /**
  52. * Length of the complete extra field in the central directory.
  53. *
  54. * @return The CentralDirectoryLength value
  55. */
  56. public ZipShort getCentralDirectoryLength() {
  57. return centralDirectoryData == null
  58. ? getLocalFileDataLength()
  59. : new ZipShort(centralDirectoryData.length);
  60. }
  61. /**
  62. * The actual data to put into local file data.
  63. *
  64. * @return The LocalFileDataData value
  65. */
  66. public byte[] getLocalFileDataData() {
  67. return ZipUtil.copy(localFileData);
  68. }
  69. /**
  70. * The actual data to put into central directory.
  71. *
  72. * @return The CentralDirectoryData value
  73. */
  74. public byte[] getCentralDirectoryData() {
  75. return centralDirectoryData == null
  76. ? getLocalFileDataData() : ZipUtil.copy(centralDirectoryData);
  77. }
  78. /**
  79. * Populate data from this array as if it was in local file data.
  80. *
  81. * @param buffer the buffer to read data from
  82. * @param offset offset into buffer to read data
  83. * @param length the length of data
  84. */
  85. public void parseFromLocalFileData(byte[] buffer, int offset, int length) {
  86. localFileData = new byte[length];
  87. System.arraycopy(buffer, offset, localFileData, 0, length);
  88. }
  89. /**
  90. * Populate data from this array as if it was in central directory data.
  91. *
  92. * @param buffer the buffer to read data from
  93. * @param offset offset into buffer to read data
  94. * @param length the length of data
  95. */
  96. public void parseFromCentralDirectoryData(byte[] buffer, int offset,
  97. int length) {
  98. centralDirectoryData = new byte[length];
  99. System.arraycopy(buffer, offset, centralDirectoryData, 0, length);
  100. if (localFileData == null) {
  101. parseFromLocalFileData(buffer, offset, length);
  102. }
  103. }
  104. }