1 17 18 package de.schlichtherle.util.zip; 19 20 import java.util.Calendar ; 21 import java.util.Date ; 22 import java.util.GregorianCalendar ; 23 import java.util.LinkedList ; 24 import java.util.zip.ZipException ; 25 26 34 public class ZipEntry implements ZipConstants, Cloneable { 35 private String name; 36 private short platform = PLATFORM_FAT; private short method = -1; private long time = -1; private long crc = -1; private long csize = -1; private long size = -1; private byte[] extra; private String comment; 45 46 long offset = -1; 47 48 51 public ZipEntry(String name) { 52 if (name == null) 53 throw new NullPointerException (); 54 if (name.length() > 0xFFFF) 55 throw new IllegalArgumentException ("Entry name too long!"); 56 this.name = name; 57 } 58 59 62 public ZipEntry(ZipEntry entry) { 63 name = entry.name; 64 method = entry.method; 65 time = entry.time; 66 crc = entry.crc; 67 size = entry.size; 68 csize = entry.csize; 69 extra = entry.extra; 70 comment = entry.comment; 71 offset = entry.offset; 72 } 73 74 77 public Object clone() { 78 try { 79 final ZipEntry entry = (ZipEntry) super.clone(); 80 entry.extra = extra != null ? (byte[]) extra.clone() : null; 81 return entry; 82 } 83 catch (CloneNotSupportedException cannotHappen) { 84 throw new AssertionError (cannotHappen); } 86 } 87 88 89 public String getName() { 90 return name; 91 } 92 93 98 protected void setName(String name) { 99 this.name = name; 100 } 101 102 106 public boolean isDirectory() { 107 return name.endsWith("/"); 108 } 109 110 public short getPlatform() { 111 return platform; 112 } 113 114 public void setPlatform(short platform) { 115 this.platform = platform; 116 } 117 118 public short getMethod() { 119 return method; 120 } 121 122 public void setMethod(short method) { 123 if (method != STORED && method != DEFLATED) 124 throw new IllegalArgumentException (name 125 + ": Invalid entry compression method!"); 126 this.method = method; 127 } 128 129 protected long getDosTime() { 130 return time; 131 } 132 133 protected void setDosTime(long time) { 134 if (time < 0) 135 throw new IllegalArgumentException (name 136 + ": Invalid entry modification time!"); 137 this.time = time; 138 } 139 140 public long getTime() { 141 return time != -1 ? dos2javaTime(time) : -1; 142 } 143 144 public void setTime(long time) { 145 if (time < 0) 146 throw new IllegalArgumentException (name 147 + ": Invalid entry modification time!"); 148 this.time = time != -1 ? java2dosTime(time) : -1; 149 } 150 151 public long getCrc() { 152 return crc; 153 } 154 155 public void setCrc(long crc) { 156 if (crc < 0 || 0xFFFFFFFFl < crc) 157 throw new IllegalArgumentException (name 158 + ": Invalid entry crc!"); 159 this.crc = crc; 160 } 161 162 public long getCompressedSize() { 163 return csize; 164 } 165 166 public void setCompressedSize(long csize) { 167 if (csize < 0 || 0xFFFFFFFFl < csize) 168 throw new IllegalArgumentException (name 169 + ": Invalid entry compressed size!"); 170 this.csize = csize; 171 } 172 173 public long getSize() { 174 return size; 175 } 176 177 public void setSize(long size) { 178 if (size < 0 || 0xFFFFFFFFl < size) 179 throw new IllegalArgumentException (name 180 + ": Invalid entry size!"); 181 this.size = size; 182 } 183 184 public byte[] getExtra() { 185 return extra != null ? (byte[]) extra.clone() : null; 186 } 187 188 public void setExtra(byte[] extra) { 189 if (extra != null && 0xFFFF < extra.length) 190 throw new IllegalArgumentException (name 191 + ": Invalid entry extra field length!"); 192 this.extra = extra; 193 } 194 195 public String getComment() { 196 return comment; 197 } 198 199 public void setComment(String comment) { 200 if (comment != null && 0xFFFF < comment.length()) 201 throw new IllegalArgumentException (name 202 + ": Invalid entry comment length!"); 203 this.comment = comment; 204 } 205 206 209 public String toString() { 210 return getName(); 211 } 212 213 217 224 protected static long dos2javaTime(long dosTime) { 225 final Calendar cal = (Calendar ) calendar.get(); 226 cal.set(Calendar.YEAR, (int) ((dosTime >> 25) & 0xff) + 1980); 227 cal.set(Calendar.MONTH, (int) ((dosTime >> 21) & 0x0f) - 1); 228 cal.set(Calendar.DATE, (int) (dosTime >> 16) & 0x1f); 229 cal.set(Calendar.HOUR_OF_DAY, (int) (dosTime >> 11) & 0x1f); 230 cal.set(Calendar.MINUTE, (int) (dosTime >> 5) & 0x3f); 231 cal.set(Calendar.SECOND, (int) (dosTime << 1) & 0x3e); 232 cal.set(Calendar.MILLISECOND, 0); 237 return cal.getTimeInMillis(); 238 } 239 240 247 protected static long java2dosTime(long time) { 248 final Calendar cal = (Calendar ) calendar.get(); 249 cal.setTimeInMillis(time); 250 int year = cal.get(Calendar.YEAR); 251 if (year < 1980) 252 return MIN_DOS_TIME; 253 return (((year - 1980) & 0xff) << 25) 254 | ((cal.get(Calendar.MONTH) + 1) << 21) 255 | (cal.get(Calendar.DAY_OF_MONTH) << 16) 256 | (cal.get(Calendar.HOUR_OF_DAY) << 11) 257 | (cal.get(Calendar.MINUTE) << 5) 258 | (cal.get(Calendar.SECOND) >> 1); 259 } 260 261 private static final ThreadLocal calendar = new ThreadLocal () { 262 protected Object initialValue() { 263 return new GregorianCalendar (); 264 } 265 }; 266 } | Popular Tags |