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.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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.io.UnsupportedEncodingException;
  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 commentin
  41. * <code>bytes</code>.
  42. */
  43. protected AbstractUnicodeExtraField(String text, byte[] bytes, int off,
  44. int len) {
  45. CRC32 crc32 = new CRC32();
  46. crc32.update(bytes, off, len);
  47. nameCRC32 = crc32.getValue();
  48. try {
  49. unicodeName = text.getBytes("UTF-8");
  50. } catch (UnsupportedEncodingException e) {
  51. throw new RuntimeException("FATAL: UTF-8 encoding not supported.",
  52. e);
  53. }
  54. }
  55. /**
  56. * Assemble as unicode extension from the name/comment and
  57. * encoding of the original zip entry.
  58. *
  59. * @param text The file name or comment.
  60. * @param bytes The encoded of the filename or comment in the zip
  61. * file.
  62. */
  63. protected AbstractUnicodeExtraField(String text, byte[] bytes) {
  64. this(text, bytes, 0, bytes.length);
  65. }
  66. private void assembleData() {
  67. if (unicodeName == null) {
  68. return;
  69. }
  70. data = new byte[5 + unicodeName.length];
  71. // version 1
  72. data[0] = 0x01;
  73. System.arraycopy(ZipLong.getBytes(nameCRC32), 0, data, 1, 4);
  74. System.arraycopy(unicodeName, 0, data, 5, unicodeName.length);
  75. }
  76. /**
  77. * @return The CRC32 checksum of the filename or comment as
  78. * encoded in the central directory of the zip file.
  79. */
  80. public long getNameCRC32() {
  81. return nameCRC32;
  82. }
  83. /**
  84. * @param nameCRC32 The CRC32 checksum of the filename as encoded
  85. * in the central directory of the zip file to set.
  86. */
  87. public void setNameCRC32(long nameCRC32) {
  88. this.nameCRC32 = nameCRC32;
  89. data = null;
  90. }
  91. /**
  92. * @return The utf-8 encoded name.
  93. */
  94. public byte[] getUnicodeName() {
  95. byte[] b = null;
  96. if (unicodeName != null) {
  97. b = new byte[unicodeName.length];
  98. System.arraycopy(unicodeName, 0, b, 0, b.length);
  99. }
  100. return b;
  101. }
  102. /**
  103. * @param unicodeName The utf-8 encoded name to set.
  104. */
  105. public void setUnicodeName(byte[] unicodeName) {
  106. if (unicodeName != null) {
  107. this.unicodeName = new byte[unicodeName.length];
  108. System.arraycopy(unicodeName, 0, this.unicodeName, 0,
  109. unicodeName.length);
  110. } else {
  111. this.unicodeName = null;
  112. }
  113. data = null;
  114. }
  115. /** {@inheritDoc} */
  116. public byte[] getCentralDirectoryData() {
  117. if (data == null) {
  118. this.assembleData();
  119. }
  120. byte[] b = null;
  121. if (data != null) {
  122. b = new byte[data.length];
  123. System.arraycopy(data, 0, b, 0, b.length);
  124. }
  125. return b;
  126. }
  127. /** {@inheritDoc} */
  128. public ZipShort getCentralDirectoryLength() {
  129. if (data == null) {
  130. assembleData();
  131. }
  132. return new ZipShort(data.length);
  133. }
  134. /** {@inheritDoc} */
  135. public byte[] getLocalFileDataData() {
  136. return getCentralDirectoryData();
  137. }
  138. /** {@inheritDoc} */
  139. public ZipShort getLocalFileDataLength() {
  140. return getCentralDirectoryLength();
  141. }
  142. /** {@inheritDoc} */
  143. public void parseFromLocalFileData(byte[] buffer, int offset, int length)
  144. throws ZipException {
  145. if (length < 5) {
  146. throw new ZipException("UniCode path extra data must have at least"
  147. + " 5 bytes.");
  148. }
  149. int version = buffer[offset];
  150. if (version != 0x01) {
  151. throw new ZipException("Unsupported version [" + version
  152. + "] for UniCode path extra data.");
  153. }
  154. nameCRC32 = ZipLong.getValue(buffer, offset + 1);
  155. unicodeName = new byte[length - 5];
  156. System.arraycopy(buffer, offset + 5, unicodeName, 0, length - 5);
  157. data = null;
  158. }
  159. }