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 4.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 orginal 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 orginal 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. return unicodeName;
  96. }
  97. /**
  98. * @param unicodeName The utf-8 encoded name to set.
  99. */
  100. public void setUnicodeName(byte[] unicodeName) {
  101. this.unicodeName = unicodeName;
  102. data = null;
  103. }
  104. public byte[] getCentralDirectoryData() {
  105. if (data == null) {
  106. this.assembleData();
  107. }
  108. return data;
  109. }
  110. public ZipShort getCentralDirectoryLength() {
  111. if (data == null) {
  112. assembleData();
  113. }
  114. return new ZipShort(data.length);
  115. }
  116. public byte[] getLocalFileDataData() {
  117. return getCentralDirectoryData();
  118. }
  119. public ZipShort getLocalFileDataLength() {
  120. return getCentralDirectoryLength();
  121. }
  122. public void parseFromLocalFileData(byte[] buffer, int offset, int length)
  123. throws ZipException {
  124. if (length < 5) {
  125. throw new ZipException("UniCode path extra data must have at least"
  126. + " 5 bytes.");
  127. }
  128. int version = buffer[offset];
  129. if (version != 0x01) {
  130. throw new ZipException("Unsupported version [" + version
  131. + "] for UniCode path extra data.");
  132. }
  133. nameCRC32 = ZipLong.getValue(buffer, offset + 1);
  134. unicodeName = new byte[length - 5];
  135. System.arraycopy(buffer, offset + 5, unicodeName, 0, length - 5);
  136. data = null;
  137. }
  138. }