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 link
29 * This class uses the ASi extra field in the format:
32 * Value Size Description
33 * ----- ---- -----------
34 * (Unix3) 0x756e Short tag for this extra block type
35 * TSize Short total data size for this block
36 * CRC Long CRC-32 of the remaining data
37 * Mode Short file permissions
38 * SizDev Long symlink'd size OR major/minor dev num
41 * (var.) variable symbolic link filename
44 * taken from appnote.iz (Info-ZIP note, 981119) found at <a
45 * href="ftp://ftp.uu.net/pub/archiving/zip/doc/"
46 * >ftp://ftp.uu.net/pub/archiving/zip/doc/</a>
51 * Short is two bytes and Long is four bytes in big endian byte and word order,
52 * device numbers are currently not supported.
56 public class AsiExtraField implements ZipExtraField, UnixStat, Cloneable {
58 private static final ZipShort HEADER_ID = new ZipShort(0x756E);
61 * Standard Unix stat(2) file mode.
82 * File this entry points to, if it is a symbolic link.
85 * empty string - if entry is not a symbolic link.
90 private String link = "";
93 * Is this an entry for a directory?
97 private boolean dirFlag = false;
100 * Instance used to calculate checksums.
104 private CRC32 crc = new CRC32();
106 /** Constructor for AsiExtraField. */
107 public AsiExtraField() {
113 * @return the value for the header id for this extrafield
116 public ZipShort getHeaderId() {
121 * Length of the extra field in the local file data - without Header-ID or
124 * @return a <code>ZipShort</code> for the length of the data of this extra
128 public ZipShort getLocalFileDataLength() {
129 return new ZipShort(4 // CRC
134 + getLinkedFile().getBytes().length);
138 * Delegate to local file data.
140 * @return the centralDirectory length
143 public ZipShort getCentralDirectoryLength() {
144 return getLocalFileDataLength();
148 * The actual data to put into local file data - without Header-ID or length
151 * @return get the data
154 public byte[] getLocalFileDataData() {
155 // CRC will be added later
156 byte[] data = new byte[getLocalFileDataLength().getValue() - 4];
157 System.arraycopy(ZipShort.getBytes(getMode()), 0, data, 0, 2);
159 byte[] linkArray = getLinkedFile().getBytes();
160 System.arraycopy(ZipLong.getBytes(linkArray.length), 0, data, 2, 4);
162 System.arraycopy(ZipShort.getBytes(getUserId()), 0, data, 6, 2);
163 System.arraycopy(ZipShort.getBytes(getGroupId()), 0, data, 8, 2);
165 System.arraycopy(linkArray, 0, data, 10, linkArray.length);
169 long checksum = crc.getValue();
171 byte[] result = new byte[data.length + 4];
172 System.arraycopy(ZipLong.getBytes(checksum), 0, result, 0, 4);
173 System.arraycopy(data, 0, result, 4, data.length);
178 * Delegate to local file data.
180 * @return the local file data
183 public byte[] getCentralDirectoryData() {
184 return getLocalFileDataData();
194 public void setUserId(int uid) {
201 * @return the user id
204 public int getUserId() {
215 public void setGroupId(int gid) {
222 * @return the group id
225 public int getGroupId() {
230 * Indicate that this entry is a symbolic link to the given filename.
233 * Name of the file this entry links to, empty String if it is not a
238 public void setLinkedFile(String name) {
240 mode = getMode(mode);
244 * Name of linked file
246 * @return name of the file this entry links to if it is a symbolic link, the
247 * empty string otherwise.
251 public String getLinkedFile() {
256 * Is this entry a symbolic link?
258 * @return true if this is a symbolic link
261 public boolean isLink() {
262 return getLinkedFile().length() != 0;
266 * File mode of this file.
272 public void setMode(int mode) {
273 this.mode = getMode(mode);
277 * File mode of this file.
279 * @return the file mode
282 public int getMode() {
287 * Indicate whether this entry is a directory.
290 * if true, this entry is a directory
293 public void setDirectory(boolean dirFlag) {
294 this.dirFlag = dirFlag;
295 mode = getMode(mode);
299 * Is this entry a directory?
301 * @return true if this entry is a directory
304 public boolean isDirectory() {
305 return dirFlag && !isLink();
309 * Populate data from this array as if it was in local file data.
316 * the number of bytes in the array from offset
318 * @throws ZipException
321 public void parseFromLocalFileData(byte[] data, int offset, int length)
322 throws ZipException {
324 long givenChecksum = ZipLong.getValue(data, offset);
325 byte[] tmp = new byte[length - 4];
326 System.arraycopy(data, offset + 4, tmp, 0, length - 4);
329 long realChecksum = crc.getValue();
330 if (givenChecksum != realChecksum) {
331 throw new ZipException("bad CRC checksum "
332 + Long.toHexString(givenChecksum) + " instead of "
333 + Long.toHexString(realChecksum));
336 int newMode = ZipShort.getValue(tmp, 0);
337 byte[] linkArray = new byte[(int) ZipLong.getValue(tmp, 2)];
338 uid = ZipShort.getValue(tmp, 6);
339 gid = ZipShort.getValue(tmp, 8);
341 if (linkArray.length == 0) {
344 System.arraycopy(tmp, 10, linkArray, 0, linkArray.length);
345 link = new String(linkArray);
347 setDirectory((newMode & DIR_FLAG) != 0);
352 * Get the file mode for given permissions with the correct file type.
356 * @return the type with the mode
359 protected int getMode(int mode) {
360 int type = FILE_FLAG;
363 } else if (isDirectory()) {
366 return type | (mode & PERM_MASK);