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.

AbstractUnicodeExtraField.java 5.5 kB

11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. import java.nio.charset.StandardCharsets;
  20. import java.util.zip.CRC32;
  21. import java.util.zip.ZipException;
  22. /**
  23. * A common base class for Unicode extra information extra fields.
  24. */
  25. public abstract class AbstractUnicodeExtraField implements ZipExtraField {
  26. private long nameCRC32;
  27. private byte[] unicodeName;
  28. private byte[] data;
  29. protected AbstractUnicodeExtraField() {
  30. }
  31. /**
  32. * Assemble as unicode extension from the name/comment and
  33. * encoding of the original zip entry.
  34. *
  35. * @param text The file name or comment.
  36. * @param bytes The encoded of the filename or comment in the zip
  37. * file.
  38. * @param off The offset of the encoded filename or comment in
  39. * <code>bytes</code>.
  40. * @param len The length of the encoded filename or comment in
  41. * <code>bytes</code>.
  42. */
  43. protected AbstractUnicodeExtraField(final String text, final byte[] bytes, final int off,
  44. final int len) {
  45. final CRC32 crc32 = new CRC32();
  46. crc32.update(bytes, off, len);
  47. nameCRC32 = crc32.getValue();
  48. unicodeName = text.getBytes(StandardCharsets.UTF_8);
  49. }
  50. /**
  51. * Assemble as unicode extension from the name/comment and
  52. * encoding of the original zip entry.
  53. *
  54. * @param text The file name or comment.
  55. * @param bytes The encoded of the filename or comment in the zip
  56. * file.
  57. */
  58. protected AbstractUnicodeExtraField(final String text, final byte[] bytes) {
  59. this(text, bytes, 0, bytes.length);
  60. }
  61. private void assembleData() {
  62. if (unicodeName == null) {
  63. return;
  64. }
  65. data = new byte[5 + unicodeName.length];
  66. // version 1
  67. data[0] = 0x01;
  68. System.arraycopy(ZipLong.getBytes(nameCRC32), 0, data, 1, 4);
  69. System.arraycopy(unicodeName, 0, data, 5, unicodeName.length);
  70. }
  71. /**
  72. * @return The CRC32 checksum of the filename or comment as
  73. * encoded in the central directory of the zip file.
  74. */
  75. public long getNameCRC32() {
  76. return nameCRC32;
  77. }
  78. /**
  79. * @param nameCRC32 The CRC32 checksum of the filename as encoded
  80. * in the central directory of the zip file to set.
  81. */
  82. public void setNameCRC32(final long nameCRC32) {
  83. this.nameCRC32 = nameCRC32;
  84. data = null;
  85. }
  86. /**
  87. * @return The utf-8 encoded name.
  88. */
  89. public byte[] getUnicodeName() {
  90. byte[] b = null;
  91. if (unicodeName != null) {
  92. b = new byte[unicodeName.length];
  93. System.arraycopy(unicodeName, 0, b, 0, b.length);
  94. }
  95. return b;
  96. }
  97. /**
  98. * @param unicodeName The utf-8 encoded name to set.
  99. */
  100. public void setUnicodeName(final byte[] unicodeName) {
  101. if (unicodeName != null) {
  102. this.unicodeName = new byte[unicodeName.length];
  103. System.arraycopy(unicodeName, 0, this.unicodeName, 0,
  104. unicodeName.length);
  105. } else {
  106. this.unicodeName = null;
  107. }
  108. data = null;
  109. }
  110. /** {@inheritDoc} */
  111. public byte[] getCentralDirectoryData() {
  112. if (data == null) {
  113. this.assembleData();
  114. }
  115. byte[] b = null;
  116. if (data != null) {
  117. b = new byte[data.length];
  118. System.arraycopy(data, 0, b, 0, b.length);
  119. }
  120. return b;
  121. }
  122. /** {@inheritDoc} */
  123. public ZipShort getCentralDirectoryLength() {
  124. if (data == null) {
  125. assembleData();
  126. }
  127. return new ZipShort(data.length);
  128. }
  129. /** {@inheritDoc} */
  130. public byte[] getLocalFileDataData() {
  131. return getCentralDirectoryData();
  132. }
  133. /** {@inheritDoc} */
  134. public ZipShort getLocalFileDataLength() {
  135. return getCentralDirectoryLength();
  136. }
  137. /** {@inheritDoc} */
  138. public void parseFromLocalFileData(final byte[] buffer, final int offset, final int length)
  139. throws ZipException {
  140. if (length < 5) {
  141. throw new ZipException("UniCode path extra data must have at least"
  142. + " 5 bytes.");
  143. }
  144. final int version = buffer[offset];
  145. if (version != 0x01) {
  146. throw new ZipException("Unsupported version [" + version
  147. + "] for UniCode path extra data.");
  148. }
  149. nameCRC32 = ZipLong.getValue(buffer, offset + 1);
  150. unicodeName = new byte[length - 5];
  151. System.arraycopy(buffer, offset + 5, unicodeName, 0, length - 5);
  152. data = null;
  153. }
  154. }