1. /*
  2. * @(#)ZipFile.java 1.58 03/01/23
  3. *
  4. * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package java.util.zip;
  8. import java.io.InputStream;
  9. import java.io.IOException;
  10. import java.io.EOFException;
  11. import java.io.File;
  12. import java.util.Vector;
  13. import java.util.Enumeration;
  14. import java.util.NoSuchElementException;
  15. import java.security.AccessController;
  16. /**
  17. * This class is used to read entries from a zip file.
  18. *
  19. * @version 1.58, 01/23/03
  20. * @author David Connelly
  21. */
  22. public
  23. class ZipFile implements ZipConstants {
  24. private long jzfile; // address of jzfile data
  25. private String name; // zip file name
  26. private int total; // total number of entries
  27. private static final int STORED = ZipEntry.STORED;
  28. private static final int DEFLATED = ZipEntry.DEFLATED;
  29. /**
  30. * Mode flag to open a zip file for reading.
  31. */
  32. public static final int OPEN_READ = 0x1;
  33. /**
  34. * Mode flag to open a zip file and mark it for deletion. The file will be
  35. * deleted some time between the moment that it is opened and the moment
  36. * that it is closed, but its contents will remain accessible via the
  37. * <tt>ZipFile</tt> object until either the close method is invoked or the
  38. * virtual machine exits.
  39. */
  40. public static final int OPEN_DELETE = 0x4;
  41. static {
  42. AccessController.doPrivileged(
  43. new sun.security.action.LoadLibraryAction("zip"));
  44. initIDs();
  45. }
  46. private static native void initIDs();
  47. /**
  48. * Opens a zip file for reading.
  49. *
  50. * <p>First, if there is a security
  51. * manager, its <code>checkRead</code> method
  52. * is called with the <code>name</code> argument
  53. * as its argument to ensure the read is allowed.
  54. *
  55. * @param name the name of the zip file
  56. * @exception ZipException if a ZIP format error has occurred
  57. * @exception IOException if an I/O error has occurred
  58. * @exception SecurityException if a security manager exists and its
  59. * <code>checkRead</code> method doesn't allow read access to the file.
  60. * @see SecurityManager#checkRead(java.lang.String)
  61. */
  62. public ZipFile(String name) throws IOException {
  63. this(new File(name), OPEN_READ);
  64. }
  65. /**
  66. * Opens a new <code>ZipFile</code> to read from the specified
  67. * <code>File</code> object in the specified mode. The mode argument
  68. * must be either <tt>OPEN_READ</tt> or <tt>OPEN_READ | OPEN_DELETE</tt>.
  69. *
  70. * <p>First, if there is a security manager, its <code>checkRead</code>
  71. * method is called with the <code>name</code> argument as its argument to
  72. * ensure the read is allowed.
  73. *
  74. * @param file the ZIP file to be opened for reading
  75. * @param mode the mode in which the file is to be opened
  76. * @exception ZipException if a ZIP format error has occurred
  77. * @exception IOException if an I/O error has occurred
  78. * @exception SecurityException if a security manager exists and its
  79. * <code>checkRead</code> method doesn't allow read access to the file,
  80. * or <code>checkDelete</code> method doesn't allow deleting the file
  81. * when <tt>OPEN_DELETE</tt> flag is set.
  82. * @exception IllegalArgumentException
  83. * If the <tt>mode</tt> argument is invalid
  84. * @see SecurityManager#checkRead(java.lang.String)
  85. */
  86. public ZipFile(File file, int mode) throws IOException {
  87. if (((mode & OPEN_READ) == 0) ||
  88. ((mode & ~(OPEN_READ | OPEN_DELETE)) != 0)) {
  89. throw new IllegalArgumentException("Illegal mode: 0x"+
  90. Integer.toHexString(mode));
  91. }
  92. String name = file.getPath();
  93. SecurityManager sm = System.getSecurityManager();
  94. if (sm != null) {
  95. sm.checkRead(name);
  96. if ((mode & OPEN_DELETE) != 0) {
  97. sm.checkDelete(name);
  98. }
  99. }
  100. long jzfileCopy = open(name, mode, file.lastModified());
  101. this.name = name;
  102. this.total = getTotal(jzfileCopy);
  103. jzfile = jzfileCopy;
  104. }
  105. private static native long open(String name, int mode, long lastModified);
  106. private static native int getTotal(long jzfile);
  107. /**
  108. * Opens a ZIP file for reading given the specified File object.
  109. * @param file the ZIP file to be opened for reading
  110. * @exception ZipException if a ZIP error has occurred
  111. * @exception IOException if an I/O error has occurred
  112. */
  113. public ZipFile(File file) throws ZipException, IOException {
  114. this(file, OPEN_READ);
  115. }
  116. /**
  117. * Returns the zip file entry for the specified name, or null
  118. * if not found.
  119. *
  120. * @param name the name of the entry
  121. * @return the zip file entry, or null if not found
  122. * @exception IllegalStateException if the zip file has been closed
  123. */
  124. public ZipEntry getEntry(String name) {
  125. if (name == null) {
  126. throw new NullPointerException("name");
  127. }
  128. long jzentry = 0;
  129. synchronized (this) {
  130. ensureOpen(jzfile);
  131. jzentry = getEntry(jzfile, name);
  132. if (jzentry == 0 && !name.endsWith("/")) {
  133. // try a directory name
  134. jzentry = getEntry(jzfile, name + "/");
  135. }
  136. if (jzentry != 0) {
  137. ZipEntry ze = new ZipEntry(name, jzentry);
  138. freeEntry(jzfile, jzentry);
  139. return ze;
  140. }
  141. }
  142. return null;
  143. }
  144. private static native long getEntry(long jzfile, String name);
  145. // freeEntry releases the C jzentry struct.
  146. private static native void freeEntry(long jzfile, long jzentry);
  147. /**
  148. * Returns an input stream for reading the contents of the specified
  149. * zip file entry.
  150. *
  151. * Returns an input stream for reading the contents of the specified
  152. * zip file entry.
  153. *
  154. * <p> Closing this ZIP file will, in turn, close all input
  155. * streams that have been returned by invocations of this method.
  156. *
  157. * @param entry the zip file entry
  158. * @return the input stream for reading the contents of the specified
  159. * zip file entry.
  160. * @exception ZipException if a ZIP format error has occurred
  161. * @exception IOException if an I/O error has occurred
  162. * @exception IllegalStateException if the zip file has been closed
  163. */
  164. public InputStream getInputStream(ZipEntry entry) throws IOException {
  165. return getInputStream(entry.name);
  166. }
  167. /**
  168. * Returns an input stream for reading the contents of the specified
  169. * entry, or null if the entry was not found.
  170. */
  171. private InputStream getInputStream(String name) throws IOException {
  172. if (name == null) {
  173. throw new NullPointerException("name");
  174. }
  175. long jzentry = 0;
  176. InputStream in = null;
  177. synchronized (this) {
  178. ensureOpen(jzfile);
  179. jzentry = getEntry(jzfile, name);
  180. if (jzentry == 0) {
  181. return null;
  182. }
  183. in = new ZipFileInputStream(jzentry, this);
  184. }
  185. switch (getMethod(jzentry)) {
  186. case STORED:
  187. return in;
  188. case DEFLATED:
  189. return new InflaterInputStream(in, getInflater()) {
  190. private boolean isClosed = false;
  191. public void close() throws IOException {
  192. if (!isClosed) {
  193. releaseInflater(inf);
  194. this.in.close();
  195. isClosed = true;
  196. }
  197. }
  198. // Override fill() method to provide an extra "dummy" byte
  199. // at the end of the input stream. This is required when
  200. // using the "nowrap" Inflater option.
  201. protected void fill() throws IOException {
  202. if (eof) {
  203. throw new EOFException(
  204. "Unexpected end of ZLIB input stream");
  205. }
  206. len = this.in.read(buf, 0, buf.length);
  207. if (len == -1) {
  208. buf[0] = 0;
  209. len = 1;
  210. eof = true;
  211. }
  212. inf.setInput(buf, 0, len);
  213. }
  214. private boolean eof;
  215. public int available() throws IOException {
  216. if (super.available() != 0) {
  217. return this.in.available();
  218. } else {
  219. return 0;
  220. }
  221. }
  222. };
  223. default:
  224. throw new ZipException("invalid compression method");
  225. }
  226. }
  227. private static native int getMethod(long jzentry);
  228. /*
  229. * Gets an inflater from the list of available inflaters or allocates
  230. * a new one.
  231. */
  232. private Inflater getInflater() {
  233. synchronized (inflaters) {
  234. int size = inflaters.size();
  235. if (size > 0) {
  236. Inflater inf = (Inflater)inflaters.remove(size - 1);
  237. inf.reset();
  238. return inf;
  239. } else {
  240. return new Inflater(true);
  241. }
  242. }
  243. }
  244. /*
  245. * Releases the specified inflater to the list of available inflaters.
  246. */
  247. private void releaseInflater(Inflater inf) {
  248. synchronized (inflaters) {
  249. inflaters.add(inf);
  250. }
  251. }
  252. // List of available Inflater objects for decompression
  253. private Vector inflaters = new Vector();
  254. /**
  255. * Returns the path name of the ZIP file.
  256. * @return the path name of the ZIP file
  257. */
  258. public String getName() {
  259. return name;
  260. }
  261. /**
  262. * Returns an enumeration of the ZIP file entries.
  263. * @return an enumeration of the ZIP file entries
  264. * @exception IllegalStateException if the zip file has been closed
  265. */
  266. public Enumeration entries() {
  267. ensureOpen(jzfile);
  268. return new Enumeration() {
  269. private int i = 0;
  270. public boolean hasMoreElements() {
  271. synchronized (ZipFile.this) {
  272. if (ZipFile.this.jzfile == 0) {
  273. throw new IllegalStateException("zip file closed");
  274. }
  275. ensureOpen(ZipFile.this.jzfile);
  276. }
  277. return i < total;
  278. }
  279. public Object nextElement() throws NoSuchElementException {
  280. synchronized (ZipFile.this) {
  281. ensureOpen(ZipFile.this.jzfile);
  282. if (i >= total) {
  283. throw new NoSuchElementException();
  284. }
  285. long jzentry = getNextEntry(jzfile, i++);
  286. if (jzentry == 0) {
  287. String message;
  288. if (ZipFile.this.jzfile == 0) {
  289. message = "ZipFile concurrently closed";
  290. } else {
  291. message = getZipMessage(ZipFile.this.jzfile);
  292. }
  293. throw new InternalError("jzentry == 0" +
  294. ",\n jzfile = " + ZipFile.this.jzfile +
  295. ",\n total = " + ZipFile.this.total +
  296. ",\n name = " + ZipFile.this.name +
  297. ",\n i = " + i +
  298. ",\n message = " + message
  299. );
  300. }
  301. ZipEntry ze = new ZipEntry(jzentry);
  302. freeEntry(jzfile, jzentry);
  303. return ze;
  304. }
  305. }
  306. };
  307. }
  308. private static native long getNextEntry(long jzfile, int i);
  309. /**
  310. * Returns the number of entries in the ZIP file.
  311. * @return the number of entries in the ZIP file
  312. * @exception IllegalStateException if the zip file has been closed
  313. */
  314. public int size() {
  315. ensureOpen(jzfile);
  316. return total;
  317. }
  318. /**
  319. * Closes the ZIP file.
  320. * <p> Closing this ZIP file will close all of the input streams
  321. * previously returned by invocations of the {@link #getInputStream
  322. * getInputStream} method.
  323. *
  324. * @throws IOException if an I/O error has occured
  325. */
  326. public void close() throws IOException {
  327. synchronized (this) {
  328. if (jzfile != 0) {
  329. // Close the zip file
  330. long zf = this.jzfile;
  331. jzfile = 0;
  332. close(zf);
  333. // Release inflaters
  334. synchronized (inflaters) {
  335. int size = inflaters.size();
  336. for (int i = 0; i < size; i++) {
  337. Inflater inf = (Inflater)inflaters.get(i);
  338. inf.end();
  339. }
  340. }
  341. }
  342. }
  343. }
  344. /**
  345. * Ensures that the <code>close</code> method of this ZIP file is
  346. * called when there are no more references to it.
  347. *
  348. * <p>
  349. * Since the time when GC would invoke this method is undetermined,
  350. * it is strongly recommanded that applications invoke the <code>close</code>
  351. * method as soon they have finished accessing this <code>ZipFile</code>.
  352. * This will prevent holding up system resources for an undetermined
  353. * length of time.
  354. *
  355. * @exception IOException if an I/O error occurs.
  356. * @see java.util.zip.ZipFile#close()
  357. */
  358. protected void finalize() throws IOException {
  359. close();
  360. }
  361. private static native void close(long jzfile);
  362. private void ensureOpen(long fd) {
  363. if (fd == 0) {
  364. throw new IllegalStateException("zip file closed");
  365. }
  366. }
  367. /*
  368. * Inner class implementing the input stream used to read a zip file entry.
  369. */
  370. private class ZipFileInputStream extends InputStream {
  371. private long jzentry; // address of jzentry data
  372. private int pos; // current position within entry data
  373. private int rem; // number of remaining bytes within entry
  374. private int size; // uncompressed size of this entry
  375. private ZipFile handle; // this would prevent the zip file from being GCed
  376. ZipFileInputStream(long jzentry, ZipFile zf) {
  377. pos = 0;
  378. rem = getCSize(jzentry);
  379. size = getSize(jzentry);
  380. this.handle = zf;
  381. this.jzentry = jzentry;
  382. }
  383. public int read(byte b[], int off, int len) throws IOException {
  384. if (rem == 0) {
  385. return -1;
  386. }
  387. if (len <= 0) {
  388. return 0;
  389. }
  390. if (len > rem) {
  391. len = rem;
  392. }
  393. synchronized (ZipFile.this) {
  394. if (ZipFile.this.jzfile == 0)
  395. throw new ZipException("ZipFile closed.");
  396. len = ZipFile.read(ZipFile.this.jzfile, jzentry, pos, b,
  397. off, len);
  398. }
  399. if (len > 0) {
  400. pos += len;
  401. rem -= len;
  402. }
  403. if (rem == 0) {
  404. close();
  405. }
  406. return len;
  407. }
  408. public int read() throws IOException {
  409. byte[] b = new byte[1];
  410. if (read(b, 0, 1) == 1) {
  411. return b[0] & 0xff;
  412. } else {
  413. return -1;
  414. }
  415. }
  416. public long skip(long n) {
  417. int len = n > rem ? rem : (int)n;
  418. pos += len;
  419. rem -= len;
  420. if (rem == 0) {
  421. close();
  422. }
  423. return len;
  424. }
  425. public int available() {
  426. return size;
  427. }
  428. public void close() {
  429. rem = 0;
  430. synchronized (ZipFile.this) {
  431. if (jzentry != 0 && ZipFile.this.jzfile != 0) {
  432. freeEntry(ZipFile.this.jzfile, jzentry);
  433. jzentry = 0;
  434. }
  435. }
  436. }
  437. }
  438. private static native int read(long jzfile, long jzentry,
  439. int pos, byte[] b, int off, int len);
  440. private static native int getCSize(long jzentry);
  441. private static native int getSize(long jzentry);
  442. // Temporary add on for bug troubleshooting
  443. private static native String getZipMessage(long jzfile);
  444. }