You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@ant.apache.org by bo...@apache.org on 2003/07/03 10:30:12 UTC
cvs commit: ant/src/testcases/org/apache/tools/ant/taskdefs UnzipTest.java
bodewig 2003/07/03 01:30:12
Modified: src/main/org/apache/tools/ant/taskdefs Expand.java
src/main/org/apache/tools/zip ZipFile.java
src/testcases/org/apache/tools/ant/taskdefs UnzipTest.java
Log:
Finish implementation of ZipFile, use it in Expand (AKA <unzip>).
Revision Changes Path
1.44 +11 -41 ant/src/main/org/apache/tools/ant/taskdefs/Expand.java
Index: Expand.java
===================================================================
RCS file: /home/cvs/ant/src/main/org/apache/tools/ant/taskdefs/Expand.java,v
retrieving revision 1.43
retrieving revision 1.44
diff -u -r1.43 -r1.44
--- Expand.java 23 Jun 2003 14:56:32 -0000 1.43
+++ Expand.java 3 Jul 2003 08:30:11 -0000 1.44
@@ -56,16 +56,13 @@
import java.io.File;
import java.io.FileInputStream;
-import java.io.RandomAccessFile;
import java.io.FileOutputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.IOException;
-import java.util.Arrays;
import java.util.Date;
+import java.util.Enumeration;
import java.util.Vector;
-import java.util.zip.ZipInputStream;
-import java.util.zip.ZipEntry;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.DirectoryScanner;
import org.apache.tools.ant.Project;
@@ -74,6 +71,8 @@
import org.apache.tools.ant.types.PatternSet;
import org.apache.tools.ant.types.selectors.SelectorUtils;
import org.apache.tools.ant.util.FileUtils;
+import org.apache.tools.zip.ZipEntry;
+import org.apache.tools.zip.ZipFile;
/**
* Unzip a file.
@@ -154,37 +153,13 @@
*/
protected void expandFile(FileUtils fileUtils, File srcF, File dir) {
log("Expanding: " + srcF + " into " + dir, Project.MSG_INFO);
- ZipInputStream zis = null;
- FileInputStream fis = null;
- RandomAccessFile raf = null;
- byte[] buff = new byte[MARKER_SIZE];
+ ZipFile zf = null;
try {
- raf = new RandomAccessFile(srcF, "r");
- long offset = 0;
- int more = raf.read(buff);
- boolean foundMarker = false;
- while (more != -1 || offset < MAX_LOOKAHEAD) {
- if (Arrays.equals(buff, ZIPMARKER)) {
- foundMarker = true;
- break;
- }
- raf.seek(++offset);
- more = raf.read(buff);
- }
- raf.close();
- raf = null;
-
- fis = new FileInputStream(srcF);
- if (foundMarker && offset > 0) {
- log("found a preamble of " + offset
- + " bytes, probably a self-extracting archive");
- fis.skip(offset);
- }
-
- zis = new ZipInputStream(fis);
- ZipEntry ze = null;
- while ((ze = zis.getNextEntry()) != null) {
- extractFile(fileUtils, srcF, dir, zis,
+ zf = new ZipFile(srcF, "UTF8");
+ Enumeration enum = zf.getEntries();
+ while (enum.hasMoreElements()) {
+ ZipEntry ze = (ZipEntry) enum.nextElement();
+ extractFile(fileUtils, srcF, dir, zf.getInputStream(ze),
ze.getName(), new Date(ze.getTime()),
ze.isDirectory());
}
@@ -194,14 +169,9 @@
throw new BuildException("Error while expanding " + srcF.getPath(),
ioe);
} finally {
- if (raf != null) {
- try {
- raf.close();
- } catch (IOException e) {}
- }
- if (zis != null) {
+ if (zf != null) {
try {
- zis.close();
+ zf.close();
} catch (IOException e) {}
}
}
1.2 +87 -2 ant/src/main/org/apache/tools/zip/ZipFile.java
Index: ZipFile.java
===================================================================
RCS file: /home/cvs/ant/src/main/org/apache/tools/zip/ZipFile.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- ZipFile.java 2 Jul 2003 14:36:14 -0000 1.1
+++ ZipFile.java 3 Jul 2003 08:30:11 -0000 1.2
@@ -63,6 +63,8 @@
import java.util.Date;
import java.util.Enumeration;
import java.util.Hashtable;
+import java.util.zip.Inflater;
+import java.util.zip.InflaterInputStream;
import java.util.zip.ZipException;
/**
@@ -202,7 +204,22 @@
*/
public InputStream getInputStream(ZipEntry ze)
throws IOException, ZipException {
- return null;
+ Long start = (Long) dataOffsets.get(ze);
+ if (start == null) {
+ return null;
+ }
+ BoundedInputStream bis =
+ new BoundedInputStream(start.longValue(), ze.getCompressedSize());
+ switch (ze.getMethod()) {
+ case ZipEntry.STORED:
+ return bis;
+ case ZipEntry.DEFLATED:
+ bis.addDummy();
+ return new InflaterInputStream(bis, new Inflater(true));
+ default:
+ throw new ZipException("Found unsupported compression method "
+ + ze.getMethod());
+ }
}
private static final int CFH_LEN =
@@ -411,6 +428,74 @@
} catch (UnsupportedEncodingException uee) {
throw new ZipException(uee.getMessage());
}
+ }
+ }
+
+ /**
+ * InputStream that delegates requests to the underlying
+ * RandomAccessFile, making sure that only bytes from a certain
+ * range can be read.
+ */
+ private class BoundedInputStream extends InputStream {
+ private long start, remaining;
+ private long loc;
+ private boolean addDummyByte = false;
+
+ BoundedInputStream(long start, long remaining) {
+ this.start = start;
+ this.remaining = remaining;
+ loc = start;
+ }
+
+ public int read() throws IOException {
+ if (remaining-- <= 0) {
+ if (addDummyByte) {
+ addDummyByte = false;
+ return 0;
+ }
+ return -1;
+ }
+ synchronized (archive) {
+ archive.seek(loc++);
+ return archive.read();
+ }
+ }
+
+ public int read(byte[] b, int off, int len) throws IOException {
+ if (remaining <= 0) {
+ if (addDummyByte) {
+ addDummyByte = false;
+ b[off] = 0;
+ return 1;
+ }
+ return -1;
+ }
+
+ if (len <= 0) {
+ return 0;
+ }
+
+ if (len > remaining) {
+ len = (int) remaining;
+ }
+ int ret = -1;
+ synchronized (archive) {
+ archive.seek(loc);
+ ret = archive.read(b, off, len);
+ }
+ if (ret > 0) {
+ loc += ret;
+ remaining -= ret;
+ }
+ return ret;
+ }
+
+ /**
+ * Inflater needs an extra dummy byte for nowrap - see
+ * Inflater's javadocs.
+ */
+ void addDummy() {
+ addDummyByte = true;
}
}
1.11 +1 -3 ant/src/testcases/org/apache/tools/ant/taskdefs/UnzipTest.java
Index: UnzipTest.java
===================================================================
RCS file: /home/cvs/ant/src/testcases/org/apache/tools/ant/taskdefs/UnzipTest.java,v
retrieving revision 1.10
retrieving revision 1.11
diff -u -r1.10 -r1.11
--- UnzipTest.java 23 Jun 2003 14:47:02 -0000 1.10
+++ UnzipTest.java 3 Jul 2003 08:30:11 -0000 1.11
@@ -138,9 +138,7 @@
* PR 16213
*/
public void testSelfExtractingArchive() {
- expectLogContaining("selfExtractingArchive",
- "found a preamble of 38439 bytes, "
- + "probably a self-extracting archive");
+ executeTarget("selfExtractingArchive");
}
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@ant.apache.org
For additional commands, e-mail: dev-help@ant.apache.org