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.

UnrecognizedExtraField.java 4.7 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. package org.apache.tools.zip;
  19. /**
  20. * Simple placeholder for all those extra fields we don't want to deal
  21. * with.
  22. *
  23. * <p>Assumes local file data and central directory entries are
  24. * identical - unless told the opposite.</p>
  25. *
  26. */
  27. public class UnrecognizedExtraField
  28. implements CentralDirectoryParsingZipExtraField {
  29. /**
  30. * The Header-ID.
  31. *
  32. * @since 1.1
  33. */
  34. private ZipShort headerId;
  35. /**
  36. * Set the header id.
  37. * @param headerId the header id to use
  38. */
  39. public void setHeaderId(ZipShort headerId) {
  40. this.headerId = headerId;
  41. }
  42. /**
  43. * Get the header id.
  44. * @return the header id
  45. */
  46. public ZipShort getHeaderId() {
  47. return headerId;
  48. }
  49. /**
  50. * Extra field data in local file data - without
  51. * Header-ID or length specifier.
  52. *
  53. * @since 1.1
  54. */
  55. private byte[] localData;
  56. /**
  57. * Set the extra field data in the local file data -
  58. * without Header-ID or length specifier.
  59. * @param data the field data to use
  60. */
  61. public void setLocalFileDataData(byte[] data) {
  62. localData = copy(data);
  63. }
  64. /**
  65. * Get the length of the local data.
  66. * @return the length of the local data
  67. */
  68. public ZipShort getLocalFileDataLength() {
  69. return new ZipShort(localData.length);
  70. }
  71. /**
  72. * Get the local data.
  73. * @return the local data
  74. */
  75. public byte[] getLocalFileDataData() {
  76. return copy(localData);
  77. }
  78. /**
  79. * Extra field data in central directory - without
  80. * Header-ID or length specifier.
  81. *
  82. * @since 1.1
  83. */
  84. private byte[] centralData;
  85. /**
  86. * Set the extra field data in central directory.
  87. * @param data the data to use
  88. */
  89. public void setCentralDirectoryData(byte[] data) {
  90. centralData = copy(data);
  91. }
  92. /**
  93. * Get the central data length.
  94. * If there is no central data, get the local file data length.
  95. * @return the central data length
  96. */
  97. public ZipShort getCentralDirectoryLength() {
  98. if (centralData != null) {
  99. return new ZipShort(centralData.length);
  100. }
  101. return getLocalFileDataLength();
  102. }
  103. /**
  104. * Get the central data.
  105. * @return the central data if present, else return the local file data
  106. */
  107. public byte[] getCentralDirectoryData() {
  108. if (centralData != null) {
  109. return copy(centralData);
  110. }
  111. return getLocalFileDataData();
  112. }
  113. /**
  114. * @param data the array of bytes.
  115. * @param offset the source location in the data array.
  116. * @param length the number of bytes to use in the data array.
  117. * @see ZipExtraField#parseFromLocalFileData(byte[], int, int)
  118. */
  119. public void parseFromLocalFileData(byte[] data, int offset, int length) {
  120. byte[] tmp = new byte[length];
  121. System.arraycopy(data, offset, tmp, 0, length);
  122. setLocalFileDataData(tmp);
  123. }
  124. /**
  125. * @param data the array of bytes.
  126. * @param offset the source location in the data array.
  127. * @param length the number of bytes to use in the data array.
  128. * @see ZipExtraField#parseFromCentralDirectoryData(byte[], int, int)
  129. */
  130. public void parseFromCentralDirectoryData(byte[] data, int offset,
  131. int length) {
  132. byte[] tmp = new byte[length];
  133. System.arraycopy(data, offset, tmp, 0, length);
  134. setCentralDirectoryData(tmp);
  135. if (localData == null) {
  136. setLocalFileDataData(tmp);
  137. }
  138. }
  139. private static byte[] copy(byte[] from) {
  140. if (from != null) {
  141. byte[] to = new byte[from.length];
  142. System.arraycopy(from, 0, to, 0, to.length);
  143. return to;
  144. }
  145. return null;
  146. }
  147. }