Browse Source

Improve algorithm that searches for the central directory - don't get fooled by signatures appearing inside payload.

git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@274750 13f79535-47bb-0310-9956-ffa450edef68
master
Stefan Bodewig 22 years ago
parent
commit
6dea74459d
2 changed files with 38 additions and 10 deletions
  1. +1
    -1
      src/main/org/apache/tools/zip/ZipEntry.java
  2. +37
    -9
      src/main/org/apache/tools/zip/ZipFile.java

+ 1
- 1
src/main/org/apache/tools/zip/ZipEntry.java View File

@@ -417,7 +417,7 @@ public class ZipEntry extends java.util.zip.ZipEntry implements Cloneable {
* @since 1.10
*/
public boolean isDirectory() {
return getName().endsWith("/");
return getName().endsWith("/");
}

protected void setName(String name) {


+ 37
- 9
src/main/org/apache/tools/zip/ZipFile.java View File

@@ -320,15 +320,42 @@ public class ZipFile {
}
}

private static final int MIN_EOCD_SIZE =
/* end of central dir signature */ 4 +
/* number of this disk */ 2 +
/* number of the disk with the */ +
/* start of the central directory */ 2 +
/* total number of entries in */ +
/* the central dir on this disk */ 2 +
/* total number of entries in */ +
/* the central dir */ 2 +
/* size of the central directory */ 4 +
/* offset of start of central */ +
/* directory with respect to */ +
/* the starting disk number */ 4 +
/* zipfile comment length */ 2;

private static final int CFD_LOCATOR_OFFSET =
/* end of central dir signature */ 4 +
/* number of this disk */ 2 +
/* number of the disk with the */ +
/* start of the central directory */ 2 +
/* total number of entries in */ +
/* the central dir on this disk */ 2 +
/* total number of entries in */ +
/* the central dir */ 2 +
/* size of the central directory */ 4;

/**
* Searches for the first occurence of the central file header
* signature.
* Searches for the "End of central dir record", parses
* it and positions the stream at the first central directory
* record.
*/
private void positionAtCentralDirectory()
throws IOException, ZipException {
archive.seek(0);
int off = 0;
byte[] sig = ZipOutputStream.CFH_SIG.getBytes();
long off = archive.length() - MIN_EOCD_SIZE;
archive.seek(off);
byte[] sig = ZipOutputStream.EOCD_SIG.getBytes();
int curr = archive.read();
boolean found = false;
while (curr != -1) {
@@ -344,16 +371,17 @@ public class ZipFile {
}
}
}
archive.seek(++off);
} else {
off++;
}
archive.seek(--off);
curr = archive.read();
}
if (!found) {
throw new ZipException("archive is not a ZIP archive");
}
archive.seek(off);
archive.seek(off + CFD_LOCATOR_OFFSET);
byte[] cfdOffset = new byte[4];
archive.readFully(cfdOffset);
archive.seek((new ZipLong(cfdOffset)).getValue());
}

/**


Loading…
Cancel
Save