ZipEntry.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. /* ZipEntry.java --
  2. Copyright (C) 2001, 2002, 2004, 2005 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package java.util.zip;
  32. import java.util.Calendar;
  33. /**
  34. * This class represents a member of a zip archive. ZipFile and
  35. * ZipInputStream will give you instances of this class as information
  36. * about the members in an archive. On the other hand ZipOutputStream
  37. * needs an instance of this class to create a new member.
  38. *
  39. * @author Jochen Hoenicke
  40. */
  41. public class ZipEntry implements ZipConstants, Cloneable
  42. {
  43. private static final byte KNOWN_SIZE = 1;
  44. private static final byte KNOWN_CSIZE = 2;
  45. private static final byte KNOWN_CRC = 4;
  46. private static final byte KNOWN_TIME = 8;
  47. private static final byte KNOWN_DOSTIME = 16;
  48. private static final byte KNOWN_EXTRA = 32;
  49. /** Immutable name of the entry */
  50. private final String name;
  51. /** Uncompressed size */
  52. private int size;
  53. /** Compressed size */
  54. private long compressedSize = -1;
  55. /** CRC of uncompressed data */
  56. private int crc;
  57. /** Comment or null if none */
  58. private String comment = null;
  59. /** The compression method. Either DEFLATED or STORED, by default -1. */
  60. private byte method = -1;
  61. /** Flags specifying what we know about this entry */
  62. private byte known = 0;
  63. /**
  64. * The 32bit DOS encoded format for the time of this entry. Only valid if
  65. * KNOWN_DOSTIME is set in known.
  66. */
  67. private int dostime;
  68. /**
  69. * The 64bit Java encoded millisecond time since the beginning of the epoch.
  70. * Only valid if KNOWN_TIME is set in known.
  71. */
  72. private long time;
  73. /** Extra data */
  74. private byte[] extra = null;
  75. int flags; /* used by ZipOutputStream */
  76. int offset; /* used by ZipFile and ZipOutputStream */
  77. /**
  78. * Compression method. This method doesn't compress at all.
  79. */
  80. public static final int STORED = 0;
  81. /**
  82. * Compression method. This method uses the Deflater.
  83. */
  84. public static final int DEFLATED = 8;
  85. /**
  86. * Creates a zip entry with the given name.
  87. * @param name the name. May include directory components separated
  88. * by '/'.
  89. *
  90. * @exception NullPointerException when name is null.
  91. * @exception IllegalArgumentException when name is bigger then 65535 chars.
  92. */
  93. public ZipEntry(String name)
  94. {
  95. int length = name.length();
  96. if (length > 65535)
  97. throw new IllegalArgumentException("name length is " + length);
  98. this.name = name;
  99. }
  100. /**
  101. * Creates a copy of the given zip entry.
  102. * @param e the entry to copy.
  103. */
  104. public ZipEntry(ZipEntry e)
  105. {
  106. this(e, e.name);
  107. }
  108. ZipEntry(ZipEntry e, String name)
  109. {
  110. this.name = name;
  111. known = e.known;
  112. size = e.size;
  113. compressedSize = e.compressedSize;
  114. crc = e.crc;
  115. dostime = e.dostime;
  116. time = e.time;
  117. method = e.method;
  118. extra = e.extra;
  119. comment = e.comment;
  120. }
  121. final void setDOSTime(int dostime)
  122. {
  123. this.dostime = dostime;
  124. known |= KNOWN_DOSTIME;
  125. known &= ~KNOWN_TIME;
  126. }
  127. final int getDOSTime()
  128. {
  129. if ((known & KNOWN_DOSTIME) != 0)
  130. return dostime;
  131. else if ((known & KNOWN_TIME) != 0)
  132. {
  133. Calendar cal = Calendar.getInstance();
  134. cal.setTimeInMillis(time);
  135. dostime = (cal.get(Calendar.YEAR) - 1980 & 0x7f) << 25
  136. | (cal.get(Calendar.MONTH) + 1) << 21
  137. | (cal.get(Calendar.DAY_OF_MONTH)) << 16
  138. | (cal.get(Calendar.HOUR_OF_DAY)) << 11
  139. | (cal.get(Calendar.MINUTE)) << 5
  140. | (cal.get(Calendar.SECOND)) >> 1;
  141. known |= KNOWN_DOSTIME;
  142. return dostime;
  143. }
  144. else
  145. return 0;
  146. }
  147. /**
  148. * Creates a copy of this zip entry.
  149. */
  150. public Object clone()
  151. {
  152. // JCL defines this as being the same as the copy constructor above,
  153. // except that value of the "extra" field is also copied. Take care
  154. // that in the case of a subclass we use clone() rather than the copy
  155. // constructor.
  156. ZipEntry clone;
  157. if (this.getClass() == ZipEntry.class)
  158. clone = new ZipEntry(this);
  159. else
  160. {
  161. try
  162. {
  163. clone = (ZipEntry) super.clone();
  164. }
  165. catch (CloneNotSupportedException e)
  166. {
  167. throw new InternalError();
  168. }
  169. }
  170. if (extra != null)
  171. {
  172. clone.extra = new byte[extra.length];
  173. System.arraycopy(extra, 0, clone.extra, 0, extra.length);
  174. }
  175. return clone;
  176. }
  177. /**
  178. * Returns the entry name. The path components in the entry are
  179. * always separated by slashes ('/').
  180. */
  181. public String getName()
  182. {
  183. return name;
  184. }
  185. /**
  186. * Sets the time of last modification of the entry.
  187. * @time the time of last modification of the entry.
  188. */
  189. public void setTime(long time)
  190. {
  191. this.time = time;
  192. this.known |= KNOWN_TIME;
  193. this.known &= ~KNOWN_DOSTIME;
  194. }
  195. /**
  196. * Gets the time of last modification of the entry.
  197. * @return the time of last modification of the entry, or -1 if unknown.
  198. */
  199. public long getTime()
  200. {
  201. // The extra bytes might contain the time (posix/unix extension)
  202. parseExtra();
  203. if ((known & KNOWN_TIME) != 0)
  204. return time;
  205. else if ((known & KNOWN_DOSTIME) != 0)
  206. {
  207. int sec = 2 * (dostime & 0x1f);
  208. int min = (dostime >> 5) & 0x3f;
  209. int hrs = (dostime >> 11) & 0x1f;
  210. int day = (dostime >> 16) & 0x1f;
  211. int mon = ((dostime >> 21) & 0xf) - 1;
  212. int year = ((dostime >> 25) & 0x7f) + 1980; /* since 1900 */
  213. try
  214. {
  215. Calendar cal = Calendar.getInstance();
  216. cal.set(year, mon, day, hrs, min, sec);
  217. time = cal.getTimeInMillis();
  218. known |= KNOWN_TIME;
  219. return time;
  220. }
  221. catch (RuntimeException ex)
  222. {
  223. /* Ignore illegal time stamp */
  224. known &= ~KNOWN_TIME;
  225. return -1;
  226. }
  227. }
  228. else
  229. return -1;
  230. }
  231. /**
  232. * Sets the size of the uncompressed data.
  233. * @exception IllegalArgumentException if size is not in 0..0xffffffffL
  234. */
  235. public void setSize(long size)
  236. {
  237. if ((size & 0xffffffff00000000L) != 0)
  238. throw new IllegalArgumentException();
  239. this.size = (int) size;
  240. this.known |= KNOWN_SIZE;
  241. }
  242. /**
  243. * Gets the size of the uncompressed data.
  244. * @return the size or -1 if unknown.
  245. */
  246. public long getSize()
  247. {
  248. return (known & KNOWN_SIZE) != 0 ? size & 0xffffffffL : -1L;
  249. }
  250. /**
  251. * Sets the size of the compressed data.
  252. */
  253. public void setCompressedSize(long csize)
  254. {
  255. this.compressedSize = csize;
  256. }
  257. /**
  258. * Gets the size of the compressed data.
  259. * @return the size or -1 if unknown.
  260. */
  261. public long getCompressedSize()
  262. {
  263. return compressedSize;
  264. }
  265. /**
  266. * Sets the crc of the uncompressed data.
  267. * @exception IllegalArgumentException if crc is not in 0..0xffffffffL
  268. */
  269. public void setCrc(long crc)
  270. {
  271. if ((crc & 0xffffffff00000000L) != 0)
  272. throw new IllegalArgumentException();
  273. this.crc = (int) crc;
  274. this.known |= KNOWN_CRC;
  275. }
  276. /**
  277. * Gets the crc of the uncompressed data.
  278. * @return the crc or -1 if unknown.
  279. */
  280. public long getCrc()
  281. {
  282. return (known & KNOWN_CRC) != 0 ? crc & 0xffffffffL : -1L;
  283. }
  284. /**
  285. * Sets the compression method. Only DEFLATED and STORED are
  286. * supported.
  287. * @exception IllegalArgumentException if method is not supported.
  288. * @see ZipOutputStream#DEFLATED
  289. * @see ZipOutputStream#STORED
  290. */
  291. public void setMethod(int method)
  292. {
  293. if (method != ZipOutputStream.STORED
  294. && method != ZipOutputStream.DEFLATED)
  295. throw new IllegalArgumentException();
  296. this.method = (byte) method;
  297. }
  298. /**
  299. * Gets the compression method.
  300. * @return the compression method or -1 if unknown.
  301. */
  302. public int getMethod()
  303. {
  304. return method;
  305. }
  306. /**
  307. * Sets the extra data.
  308. * @exception IllegalArgumentException if extra is longer than 0xffff bytes.
  309. */
  310. public void setExtra(byte[] extra)
  311. {
  312. if (extra == null)
  313. {
  314. this.extra = null;
  315. return;
  316. }
  317. if (extra.length > 0xffff)
  318. throw new IllegalArgumentException();
  319. this.extra = extra;
  320. }
  321. private void parseExtra()
  322. {
  323. // Already parsed?
  324. if ((known & KNOWN_EXTRA) != 0)
  325. return;
  326. if (extra == null)
  327. {
  328. known |= KNOWN_EXTRA;
  329. return;
  330. }
  331. try
  332. {
  333. int pos = 0;
  334. while (pos < extra.length)
  335. {
  336. int sig = (extra[pos++] & 0xff)
  337. | (extra[pos++] & 0xff) << 8;
  338. int len = (extra[pos++] & 0xff)
  339. | (extra[pos++] & 0xff) << 8;
  340. if (sig == 0x5455)
  341. {
  342. /* extended time stamp */
  343. int flags = extra[pos];
  344. if ((flags & 1) != 0)
  345. {
  346. long time = ((extra[pos+1] & 0xff)
  347. | (extra[pos+2] & 0xff) << 8
  348. | (extra[pos+3] & 0xff) << 16
  349. | (extra[pos+4] & 0xff) << 24);
  350. setTime(time*1000);
  351. }
  352. }
  353. pos += len;
  354. }
  355. }
  356. catch (ArrayIndexOutOfBoundsException ex)
  357. {
  358. /* be lenient */
  359. }
  360. known |= KNOWN_EXTRA;
  361. return;
  362. }
  363. /**
  364. * Gets the extra data.
  365. * @return the extra data or null if not set.
  366. */
  367. public byte[] getExtra()
  368. {
  369. return extra;
  370. }
  371. /**
  372. * Sets the entry comment.
  373. * @exception IllegalArgumentException if comment is longer than 0xffff.
  374. */
  375. public void setComment(String comment)
  376. {
  377. if (comment != null && comment.length() > 0xffff)
  378. throw new IllegalArgumentException();
  379. this.comment = comment;
  380. }
  381. /**
  382. * Gets the comment.
  383. * @return the comment or null if not set.
  384. */
  385. public String getComment()
  386. {
  387. return comment;
  388. }
  389. /**
  390. * Gets true, if the entry is a directory. This is solely
  391. * determined by the name, a trailing slash '/' marks a directory.
  392. */
  393. public boolean isDirectory()
  394. {
  395. int nlen = name.length();
  396. return nlen > 0 && name.charAt(nlen - 1) == '/';
  397. }
  398. /**
  399. * Gets the string representation of this ZipEntry. This is just
  400. * the name as returned by getName().
  401. */
  402. public String toString()
  403. {
  404. return name;
  405. }
  406. /**
  407. * Gets the hashCode of this ZipEntry. This is just the hashCode
  408. * of the name. Note that the equals method isn't changed, though.
  409. */
  410. public int hashCode()
  411. {
  412. return name.hashCode();
  413. }
  414. }