Debian Codegraph @

src/main/java/org/apache/tools/zip/AbstractUnicodeExtraField.java

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