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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 package org.apache.tools.zip;
21 import java.util.zip.CRC32;
22 import java.util.zip.ZipException;
25 * Adds Unix file permission and UID/GID fields as well as symbolic
28 * <p>This class uses the ASi extra field in the format:
30 * Value Size Description
31 * ----- ---- -----------
32 * (Unix3) 0x756e Short tag for this extra block type
33 * TSize Short total data size for this block
34 * CRC Long CRC-32 of the remaining data
35 * Mode Short file permissions
36 * SizDev Long symlink'd size OR major/minor dev num
39 * (var.) variable symbolic link filename
41 * taken from appnote.iz (Info-ZIP note, 981119) found at <a
42 * href="ftp://ftp.uu.net/pub/archiving/zip/doc/">ftp://ftp.uu.net/pub/archiving/zip/doc/</a></p>
45 * <p>Short is two bytes and Long is four bytes in big endian byte and
46 * word order, device numbers are currently not supported.</p>
49 public class AsiExtraField implements ZipExtraField, UnixStat, Cloneable {
51 private static final ZipShort HEADER_ID = new ZipShort(0x756E);
54 * Standard Unix stat(2) file mode.
72 * File this entry points to, if it is a symbolic link.
74 * <p>empty string - if entry is not a symbolic link.</p>
78 private String link = "";
80 * Is this an entry for a directory?
84 private boolean dirFlag = false;
87 * Instance used to calculate checksums.
91 private CRC32 crc = new CRC32();
93 /** Constructor for AsiExtraField. */
94 public AsiExtraField() {
99 * @return the value for the header id for this extrafield
102 public ZipShort getHeaderId() {
107 * Length of the extra field in the local file data - without
108 * Header-ID or length specifier.
109 * @return a <code>ZipShort</code> for the length of the data of this extra field
112 public ZipShort getLocalFileDataLength() {
113 return new ZipShort(4 // CRC
118 + getLinkedFile().getBytes().length);
122 * Delegate to local file data.
123 * @return the centralDirectory length
126 public ZipShort getCentralDirectoryLength() {
127 return getLocalFileDataLength();
131 * The actual data to put into local file data - without Header-ID
132 * or length specifier.
133 * @return get the data
136 public byte[] getLocalFileDataData() {
137 // CRC will be added later
138 byte[] data = new byte[getLocalFileDataLength().getValue() - 4];
139 System.arraycopy(ZipShort.getBytes(getMode()), 0, data, 0, 2);
141 byte[] linkArray = getLinkedFile().getBytes();
142 System.arraycopy(ZipLong.getBytes(linkArray.length),
145 System.arraycopy(ZipShort.getBytes(getUserId()),
147 System.arraycopy(ZipShort.getBytes(getGroupId()),
150 System.arraycopy(linkArray, 0, data, 10, linkArray.length);
154 long checksum = crc.getValue();
156 byte[] result = new byte[data.length + 4];
157 System.arraycopy(ZipLong.getBytes(checksum), 0, result, 0, 4);
158 System.arraycopy(data, 0, result, 4, data.length);
163 * Delegate to local file data.
164 * @return the local file data
167 public byte[] getCentralDirectoryData() {
168 return getLocalFileDataData();
173 * @param uid the user id
176 public void setUserId(int uid) {
182 * @return the user id
185 public int getUserId() {
191 * @param gid the group id
194 public void setGroupId(int gid) {
200 * @return the group id
203 public int getGroupId() {
208 * Indicate that this entry is a symbolic link to the given filename.
210 * @param name Name of the file this entry links to, empty String
211 * if it is not a symbolic link.
215 public void setLinkedFile(String name) {
217 mode = getMode(mode);
221 * Name of linked file
223 * @return name of the file this entry links to if it is a
224 * symbolic link, the empty string otherwise.
228 public String getLinkedFile() {
233 * Is this entry a symbolic link?
234 * @return true if this is a symbolic link
237 public boolean isLink() {
238 return getLinkedFile().length() != 0;
242 * File mode of this file.
243 * @param mode the file mode
246 public void setMode(int mode) {
247 this.mode = getMode(mode);
251 * File mode of this file.
252 * @return the file mode
255 public int getMode() {
260 * Indicate whether this entry is a directory.
261 * @param dirFlag if true, this entry is a directory
264 public void setDirectory(boolean dirFlag) {
265 this.dirFlag = dirFlag;
266 mode = getMode(mode);
270 * Is this entry a directory?
271 * @return true if this entry is a directory
274 public boolean isDirectory() {
275 return dirFlag && !isLink();
279 * Populate data from this array as if it was in local file data.
280 * @param data an array of bytes
281 * @param offset the start offset
282 * @param length the number of bytes in the array from offset
284 * @throws ZipException on error
286 public void parseFromLocalFileData(byte[] data, int offset, int length)
287 throws ZipException {
289 long givenChecksum = ZipLong.getValue(data, offset);
290 byte[] tmp = new byte[length - 4];
291 System.arraycopy(data, offset + 4, tmp, 0, length - 4);
294 long realChecksum = crc.getValue();
295 if (givenChecksum != realChecksum) {
296 throw new ZipException("bad CRC checksum "
297 + Long.toHexString(givenChecksum)
299 + Long.toHexString(realChecksum));
302 int newMode = ZipShort.getValue(tmp, 0);
303 byte[] linkArray = new byte[(int) ZipLong.getValue(tmp, 2)];
304 uid = ZipShort.getValue(tmp, 6);
305 gid = ZipShort.getValue(tmp, 8);
307 if (linkArray.length == 0) {
310 System.arraycopy(tmp, 10, linkArray, 0, linkArray.length);
311 link = new String(linkArray);
313 setDirectory((newMode & DIR_FLAG) != 0);
318 * Get the file mode for given permissions with the correct file type.
319 * @param mode the mode
320 * @return the type with the mode
323 protected int getMode(int mode) {
324 int type = FILE_FLAG;
327 } else if (isDirectory()) {
330 return type | (mode & PERM_MASK);