You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

ZipFile.java 19 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  1. /*
  2. * The Apache Software License, Version 1.1
  3. *
  4. * Copyright (c) 2003 The Apache Software Foundation. All rights
  5. * reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. *
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in
  16. * the documentation and/or other materials provided with the
  17. * distribution.
  18. *
  19. * 3. The end-user documentation included with the redistribution, if
  20. * any, must include the following acknowlegement:
  21. * "This product includes software developed by the
  22. * Apache Software Foundation (http://www.apache.org/)."
  23. * Alternately, this acknowlegement may appear in the software itself,
  24. * if and wherever such third-party acknowlegements normally appear.
  25. *
  26. * 4. The names "Ant" and "Apache Software
  27. * Foundation" must not be used to endorse or promote products derived
  28. * from this software without prior written permission. For written
  29. * permission, please contact apache@apache.org.
  30. *
  31. * 5. Products derived from this software may not be called "Apache"
  32. * nor may "Apache" appear in their names without prior written
  33. * permission of the Apache Group.
  34. *
  35. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  36. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  37. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  38. * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  39. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  40. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  41. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  42. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  43. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  44. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  45. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  46. * SUCH DAMAGE.
  47. * ====================================================================
  48. *
  49. * This software consists of voluntary contributions made by many
  50. * individuals on behalf of the Apache Software Foundation. For more
  51. * information on the Apache Software Foundation, please see
  52. * <http://www.apache.org/>.
  53. */
  54. package org.apache.tools.zip;
  55. import java.io.File;
  56. import java.io.IOException;
  57. import java.io.InputStream;
  58. import java.io.RandomAccessFile;
  59. import java.io.UnsupportedEncodingException;
  60. import java.util.Calendar;
  61. import java.util.Date;
  62. import java.util.Enumeration;
  63. import java.util.Hashtable;
  64. import java.util.zip.Inflater;
  65. import java.util.zip.InflaterInputStream;
  66. import java.util.zip.ZipException;
  67. /**
  68. * Replacement for <code>java.util.ZipFile</code>.
  69. *
  70. * <p>This class adds support for file name encodings other than UTF-8
  71. * (which is required to work on ZIP files created by native zip tools
  72. * and is able to skip a preamble like the one found in self
  73. * extracting archives. Furthermore it returns instances of
  74. * <code>org.apache.tools.zip.ZipEntry</code> instead of
  75. * <code>java.util.zip.ZipEntry</code>.</p>
  76. *
  77. * <p>It doesn't extend <code>java.util.zip.ZipFile</code> as it would
  78. * have to reimplement all methods anyway. Like
  79. * <code>java.util.ZipFile</code>, it uses RandomAccessFile under the
  80. * covers and supports compressed and uncompressed entries.</p>
  81. *
  82. * <p>The method signatures mimic the ones of
  83. * <code>java.util.zip.ZipFile</code>, with a couple of exceptions:
  84. *
  85. * <ul>
  86. * <li>There is no getName method.</li>
  87. * <li>entries has been renamed to getEntries.</li>
  88. * <li>getEntries and getEntry return
  89. * <code>org.apache.tools.zip.ZipEntry</code> instances.</li>
  90. * <li>close is allowed to throw IOException.</li>
  91. * </ul>
  92. *
  93. * @author Stefan Bodewig
  94. * @version $Revision$
  95. */
  96. public class ZipFile {
  97. /**
  98. * Maps ZipEntrys to Longs, recording the offsets of the local
  99. * file headers.
  100. */
  101. private Hashtable entries = new Hashtable();
  102. /**
  103. * Maps String to ZipEntrys, name -> actual entry.
  104. */
  105. private Hashtable nameMap = new Hashtable();
  106. /**
  107. * Maps ZipEntrys to Longs, recording the offsets of the actual file data.
  108. */
  109. private Hashtable dataOffsets = new Hashtable();
  110. /**
  111. * The encoding to use for filenames and the file comment.
  112. *
  113. * <p>For a list of possible values see <a
  114. * href="http://java.sun.com/products/jdk/1.2/docs/guide/internat/encoding.doc.html">http://java.sun.com/products/jdk/1.2/docs/guide/internat/encoding.doc.html</a>.
  115. * Defaults to the platform's default character encoding.</p>
  116. */
  117. private String encoding = null;
  118. /**
  119. * The actual data source.
  120. */
  121. private RandomAccessFile archive;
  122. /**
  123. * Opens the given file for reading, assuming the platform's
  124. * native encoding for file names.
  125. *
  126. * @param f the archive.
  127. *
  128. * @throws IOException if an error occurs while reading the file.
  129. */
  130. public ZipFile(File f) throws IOException {
  131. this(f, null);
  132. }
  133. /**
  134. * Opens the given file for reading, assuming the platform's
  135. * native encoding for file names.
  136. *
  137. * @param name name of the archive.
  138. *
  139. * @throws IOException if an error occurs while reading the file.
  140. */
  141. public ZipFile(String name) throws IOException {
  142. this(new File(name), null);
  143. }
  144. /**
  145. * Opens the given file for reading, assuming the specified
  146. * encoding for file names.
  147. *
  148. * @param name name of the archive.
  149. * @param encoding the encoding to use for file names
  150. *
  151. * @throws IOException if an error occurs while reading the file.
  152. */
  153. public ZipFile(String name, String encoding) throws IOException {
  154. this(new File(name), encoding);
  155. }
  156. /**
  157. * Opens the given file for reading, assuming the specified
  158. * encoding for file names.
  159. *
  160. * @param f the archive.
  161. * @param encoding the encoding to use for file names
  162. *
  163. * @throws IOException if an error occurs while reading the file.
  164. */
  165. public ZipFile(File f, String encoding) throws IOException {
  166. this.encoding = encoding;
  167. archive = new RandomAccessFile(f, "r");
  168. populateFromCentralDirectory();
  169. resolveLocalFileHeaderData();
  170. }
  171. /**
  172. * The encoding to use for filenames and the file comment.
  173. *
  174. * @return null if using the platform's default character encoding.
  175. */
  176. public String getEncoding() {
  177. return encoding;
  178. }
  179. /**
  180. * Closes the archive.
  181. * @throws IOException if an error occurs closing the archive.
  182. */
  183. public void close() throws IOException {
  184. archive.close();
  185. }
  186. /**
  187. * Returns all entries as {@link org.apache.tools.ant.ZipEntry
  188. * ZipEntry} instances.
  189. * @return all entries as ZipEntry instances.
  190. */
  191. public Enumeration getEntries() {
  192. return entries.keys();
  193. }
  194. /**
  195. * Returns a named entry - or <code>null</code> if no entry by
  196. * that name exists.
  197. * @param name name of the entry.
  198. * @return the ZipEntry corresponding to the given name - or
  199. * <code>null</code> if not present.
  200. */
  201. public ZipEntry getEntry(String name) {
  202. return (ZipEntry) nameMap.get(name);
  203. }
  204. /**
  205. * Returns an InputStream for reading the contents of the given entry.
  206. * @param ze the entry to get the stream for.
  207. * @return a stream to read the entry from.
  208. */
  209. public InputStream getInputStream(ZipEntry ze)
  210. throws IOException, ZipException {
  211. Long start = (Long) dataOffsets.get(ze);
  212. if (start == null) {
  213. return null;
  214. }
  215. BoundedInputStream bis =
  216. new BoundedInputStream(start.longValue(), ze.getCompressedSize());
  217. switch (ze.getMethod()) {
  218. case ZipEntry.STORED:
  219. return bis;
  220. case ZipEntry.DEFLATED:
  221. bis.addDummy();
  222. return new InflaterInputStream(bis, new Inflater(true));
  223. default:
  224. throw new ZipException("Found unsupported compression method "
  225. + ze.getMethod());
  226. }
  227. }
  228. private static final int CFH_LEN =
  229. /* version made by */ 2 +
  230. /* version needed to extract */ 2 +
  231. /* general purpose bit flag */ 2 +
  232. /* compression method */ 2 +
  233. /* last mod file time */ 2 +
  234. /* last mod file date */ 2 +
  235. /* crc-32 */ 4 +
  236. /* compressed size */ 4 +
  237. /* uncompressed size */ 4 +
  238. /* filename length */ 2 +
  239. /* extra field length */ 2 +
  240. /* file comment length */ 2 +
  241. /* disk number start */ 2 +
  242. /* internal file attributes */ 2 +
  243. /* external file attributes */ 4 +
  244. /* relative offset of local header */ 4;
  245. /**
  246. * Reads the central directory of the given archive and populates
  247. * the internal tables with ZipEntry instances.
  248. *
  249. * <p>The ZipEntrys will know all data that can be obtained from
  250. * the central directory alone, but not the data that requires the
  251. * local file header or additional data to be read.</p>
  252. */
  253. private void populateFromCentralDirectory()
  254. throws IOException {
  255. positionAtCentralDirectory();
  256. byte[] cfh = new byte[CFH_LEN];
  257. byte[] signatureBytes = new byte[4];
  258. archive.readFully(signatureBytes);
  259. ZipLong sig = new ZipLong(signatureBytes);
  260. while (sig.equals(ZipOutputStream.CFH_SIG)) {
  261. archive.readFully(cfh);
  262. int off = 0;
  263. ZipEntry ze = new ZipEntry();
  264. ZipShort versionMadeBy = new ZipShort(cfh, off);
  265. off += 2;
  266. ze.setPlatform((versionMadeBy.getValue() >> 8) & 0x0F);
  267. off += 4; // skip version info and general purpose byte
  268. ze.setMethod((new ZipShort(cfh, off)).getValue());
  269. off += 2;
  270. ze.setTime(fromDosTime(new ZipLong(cfh, off)).getTime());
  271. off += 4;
  272. ze.setCrc((new ZipLong(cfh, off)).getValue());
  273. off += 4;
  274. ze.setCompressedSize((new ZipLong(cfh, off)).getValue());
  275. off += 4;
  276. ze.setSize((new ZipLong(cfh, off)).getValue());
  277. off += 4;
  278. int fileNameLen = (new ZipShort(cfh, off)).getValue();
  279. off += 2;
  280. int extraLen = (new ZipShort(cfh, off)).getValue();
  281. off += 2;
  282. int commentLen = (new ZipShort(cfh, off)).getValue();
  283. off += 2;
  284. off += 2; // disk number
  285. ze.setInternalAttributes((new ZipShort(cfh, off)).getValue());
  286. off += 2;
  287. ze.setExternalAttributes((new ZipLong(cfh, off)).getValue());
  288. off += 4;
  289. // LFH offset
  290. entries.put(ze, new Long((new ZipLong(cfh, off)).getValue()));
  291. byte[] fileName = new byte[fileNameLen];
  292. archive.readFully(fileName);
  293. ze.setName(getString(fileName));
  294. nameMap.put(ze.getName(), ze);
  295. archive.skipBytes(extraLen);
  296. byte[] comment = new byte[commentLen];
  297. archive.readFully(comment);
  298. ze.setComment(getString(comment));
  299. archive.readFully(signatureBytes);
  300. sig = new ZipLong(signatureBytes);
  301. }
  302. }
  303. private static final int MIN_EOCD_SIZE =
  304. /* end of central dir signature */ 4 +
  305. /* number of this disk */ 2 +
  306. /* number of the disk with the */ +
  307. /* start of the central directory */ 2 +
  308. /* total number of entries in */ +
  309. /* the central dir on this disk */ 2 +
  310. /* total number of entries in */ +
  311. /* the central dir */ 2 +
  312. /* size of the central directory */ 4 +
  313. /* offset of start of central */ +
  314. /* directory with respect to */ +
  315. /* the starting disk number */ 4 +
  316. /* zipfile comment length */ 2;
  317. private static final int CFD_LOCATOR_OFFSET =
  318. /* end of central dir signature */ 4 +
  319. /* number of this disk */ 2 +
  320. /* number of the disk with the */ +
  321. /* start of the central directory */ 2 +
  322. /* total number of entries in */ +
  323. /* the central dir on this disk */ 2 +
  324. /* total number of entries in */ +
  325. /* the central dir */ 2 +
  326. /* size of the central directory */ 4;
  327. /**
  328. * Searches for the &quot;End of central dir record&quot;, parses
  329. * it and positions the stream at the first central directory
  330. * record.
  331. */
  332. private void positionAtCentralDirectory()
  333. throws IOException {
  334. long off = archive.length() - MIN_EOCD_SIZE;
  335. archive.seek(off);
  336. byte[] sig = ZipOutputStream.EOCD_SIG.getBytes();
  337. int curr = archive.read();
  338. boolean found = false;
  339. while (curr != -1) {
  340. if (curr == sig[0]) {
  341. curr = archive.read();
  342. if (curr == sig[1]) {
  343. curr = archive.read();
  344. if (curr == sig[2]) {
  345. curr = archive.read();
  346. if (curr == sig[3]) {
  347. found = true;
  348. break;
  349. }
  350. }
  351. }
  352. }
  353. archive.seek(--off);
  354. curr = archive.read();
  355. }
  356. if (!found) {
  357. throw new ZipException("archive is not a ZIP archive");
  358. }
  359. archive.seek(off + CFD_LOCATOR_OFFSET);
  360. byte[] cfdOffset = new byte[4];
  361. archive.readFully(cfdOffset);
  362. archive.seek((new ZipLong(cfdOffset)).getValue());
  363. }
  364. /**
  365. * Number of bytes in local file header up to the &quot;length of
  366. * filename&quot; entry.
  367. */
  368. private static final long LFH_OFFSET_FOR_FILENAME_LENGTH =
  369. /* local file header signature */ 4 +
  370. /* version needed to extract */ 2 +
  371. /* general purpose bit flag */ 2 +
  372. /* compression method */ 2 +
  373. /* last mod file time */ 2 +
  374. /* last mod file date */ 2 +
  375. /* crc-32 */ 4 +
  376. /* compressed size */ 4 +
  377. /* uncompressed size */ 4;
  378. /**
  379. * Walks through all recorded entries and adds the data available
  380. * from the local file header.
  381. *
  382. * <p>Also records the offsets for the data to read from the
  383. * entries.</p>
  384. */
  385. private void resolveLocalFileHeaderData()
  386. throws IOException {
  387. Enumeration enum = getEntries();
  388. while (enum.hasMoreElements()) {
  389. ZipEntry ze = (ZipEntry) enum.nextElement();
  390. long offset = ((Long) entries.get(ze)).longValue();
  391. archive.seek(offset + LFH_OFFSET_FOR_FILENAME_LENGTH);
  392. byte[] b = new byte[2];
  393. archive.readFully(b);
  394. int fileNameLen = (new ZipShort(b)).getValue();
  395. archive.readFully(b);
  396. int extraFieldLen = (new ZipShort(b)).getValue();
  397. archive.skipBytes(fileNameLen);
  398. byte[] localExtraData = new byte[extraFieldLen];
  399. archive.readFully(localExtraData);
  400. ze.setExtra(localExtraData);
  401. dataOffsets.put(ze,
  402. new Long(offset + LFH_OFFSET_FOR_FILENAME_LENGTH
  403. + 2 + 2 + fileNameLen + extraFieldLen));
  404. }
  405. }
  406. /**
  407. * Convert a DOS date/time field to a Date object.
  408. *
  409. * @param l contains the stored DOS time.
  410. * @return a Date instance corresponding to the given time.
  411. */
  412. protected static Date fromDosTime(ZipLong l) {
  413. long dosTime = l.getValue();
  414. Calendar cal = Calendar.getInstance();
  415. cal.set(Calendar.YEAR, (int) ((dosTime >> 25) & 0x7f) + 1980);
  416. cal.set(Calendar.MONTH, (int) ((dosTime >> 21) & 0x0f) - 1);
  417. cal.set(Calendar.DATE, (int) (dosTime >> 16) & 0x1f);
  418. cal.set(Calendar.HOUR_OF_DAY, (int) (dosTime >> 11) & 0x1f);
  419. cal.set(Calendar.MINUTE, (int) (dosTime >> 5) & 0x3f);
  420. cal.set(Calendar.SECOND, (int) (dosTime << 1) & 0x3e);
  421. return cal.getTime();
  422. }
  423. /**
  424. * Retrieve a String from the given bytes using the encoding set
  425. * for this ZipFile.
  426. *
  427. * @param bytes the byte array to transform
  428. * @return String obtained by using the given encoding
  429. * @throws ZipException if the encoding cannot be recognized.
  430. */
  431. protected String getString(byte[] bytes) throws ZipException {
  432. if (encoding == null) {
  433. return new String(bytes);
  434. } else {
  435. try {
  436. return new String(bytes, encoding);
  437. } catch (UnsupportedEncodingException uee) {
  438. throw new ZipException(uee.getMessage());
  439. }
  440. }
  441. }
  442. /**
  443. * InputStream that delegates requests to the underlying
  444. * RandomAccessFile, making sure that only bytes from a certain
  445. * range can be read.
  446. */
  447. private class BoundedInputStream extends InputStream {
  448. private long start, remaining;
  449. private long loc;
  450. private boolean addDummyByte = false;
  451. BoundedInputStream(long start, long remaining) {
  452. this.start = start;
  453. this.remaining = remaining;
  454. loc = start;
  455. }
  456. public int read() throws IOException {
  457. if (remaining-- <= 0) {
  458. if (addDummyByte) {
  459. addDummyByte = false;
  460. return 0;
  461. }
  462. return -1;
  463. }
  464. synchronized (archive) {
  465. archive.seek(loc++);
  466. return archive.read();
  467. }
  468. }
  469. public int read(byte[] b, int off, int len) throws IOException {
  470. if (remaining <= 0) {
  471. if (addDummyByte) {
  472. addDummyByte = false;
  473. b[off] = 0;
  474. return 1;
  475. }
  476. return -1;
  477. }
  478. if (len <= 0) {
  479. return 0;
  480. }
  481. if (len > remaining) {
  482. len = (int) remaining;
  483. }
  484. int ret = -1;
  485. synchronized (archive) {
  486. archive.seek(loc);
  487. ret = archive.read(b, off, len);
  488. }
  489. if (ret > 0) {
  490. loc += ret;
  491. remaining -= ret;
  492. }
  493. return ret;
  494. }
  495. /**
  496. * Inflater needs an extra dummy byte for nowrap - see
  497. * Inflater's javadocs.
  498. */
  499. void addDummy() {
  500. addDummyByte = true;
  501. }
  502. }
  503. }