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