1 18 19 package org.apache.tools.zip; 20 21 import java.util.zip.CRC32 ; 22 import java.util.zip.ZipException ; 23 24 49 public class AsiExtraField implements ZipExtraField, UnixStat, Cloneable { 50 51 private static final ZipShort HEADER_ID = new ZipShort(0x756E); 52 53 58 private int mode = 0; 59 64 private int uid = 0; 65 70 private int gid = 0; 71 78 private String link = ""; 79 84 private boolean dirFlag = false; 85 86 91 private CRC32 crc = new CRC32 (); 92 93 94 public AsiExtraField() { 95 } 96 97 102 public ZipShort getHeaderId() { 103 return HEADER_ID; 104 } 105 106 112 public ZipShort getLocalFileDataLength() { 113 return new ZipShort(4 + 2 + 4 + 2 + 2 + getLinkedFile().getBytes().length); 119 } 120 121 126 public ZipShort getCentralDirectoryLength() { 127 return getLocalFileDataLength(); 128 } 129 130 136 public byte[] getLocalFileDataData() { 137 byte[] data = new byte[getLocalFileDataLength().getValue() - 4]; 139 System.arraycopy(ZipShort.getBytes(getMode()), 0, data, 0, 2); 140 141 byte[] linkArray = getLinkedFile().getBytes(); 142 System.arraycopy(ZipLong.getBytes(linkArray.length), 143 0, data, 2, 4); 144 145 System.arraycopy(ZipShort.getBytes(getUserId()), 146 0, data, 6, 2); 147 System.arraycopy(ZipShort.getBytes(getGroupId()), 148 0, data, 8, 2); 149 150 System.arraycopy(linkArray, 0, data, 10, linkArray.length); 151 152 crc.reset(); 153 crc.update(data); 154 long checksum = crc.getValue(); 155 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); 159 return result; 160 } 161 162 167 public byte[] getCentralDirectoryData() { 168 return getLocalFileDataData(); 169 } 170 171 176 public void setUserId(int uid) { 177 this.uid = uid; 178 } 179 180 185 public int getUserId() { 186 return uid; 187 } 188 189 194 public void setGroupId(int gid) { 195 this.gid = gid; 196 } 197 198 203 public int getGroupId() { 204 return gid; 205 } 206 207 215 public void setLinkedFile(String name) { 216 link = name; 217 mode = getMode(mode); 218 } 219 220 228 public String getLinkedFile() { 229 return link; 230 } 231 232 237 public boolean isLink() { 238 return getLinkedFile().length() != 0; 239 } 240 241 246 public void setMode(int mode) { 247 this.mode = getMode(mode); 248 } 249 250 255 public int getMode() { 256 return mode; 257 } 258 259 264 public void setDirectory(boolean dirFlag) { 265 this.dirFlag = dirFlag; 266 mode = getMode(mode); 267 } 268 269 274 public boolean isDirectory() { 275 return dirFlag && !isLink(); 276 } 277 278 286 public void parseFromLocalFileData(byte[] data, int offset, int length) 287 throws ZipException { 288 289 long givenChecksum = ZipLong.getValue(data, offset); 290 byte[] tmp = new byte[length - 4]; 291 System.arraycopy(data, offset + 4, tmp, 0, length - 4); 292 crc.reset(); 293 crc.update(tmp); 294 long realChecksum = crc.getValue(); 295 if (givenChecksum != realChecksum) { 296 throw new ZipException ("bad CRC checksum " 297 + Long.toHexString(givenChecksum) 298 + " instead of " 299 + Long.toHexString(realChecksum)); 300 } 301 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); 306 307 if (linkArray.length == 0) { 308 link = ""; 309 } else { 310 System.arraycopy(tmp, 10, linkArray, 0, linkArray.length); 311 link = new String (linkArray); 312 } 313 setDirectory((newMode & DIR_FLAG) != 0); 314 setMode(newMode); 315 } 316 317 323 protected int getMode(int mode) { 324 int type = FILE_FLAG; 325 if (isLink()) { 326 type = LINK_FLAG; 327 } else if (isDirectory()) { 328 type = DIR_FLAG; 329 } 330 return type | (mode & PERM_MASK); 331 } 332 333 } 334 | Popular Tags |