ZipUtil { static CentralDirectory findCentralDirectory(RandomAccessFile raf) throws IOException, ZipException { long scanOffset = raf.length() - ENDHDR; if (scanOffset < 0) { throw new ZipException("File too short to be a zip file: " + raf.length()); } long stopOffset = scanOffset - 0x10000 ; if (stopOffset < 0) { stopOffset = 0; } int endSig = Integer.reverseBytes(ENDSIG); while (true) { raf.seek(scanOffset); if (raf.readInt() == endSig) { break; } scanOffset--; if (scanOffset < stopOffset) { throw new ZipException("End Of Central Directory signature not found"); } } raf.skipBytes(2); raf.skipBytes(2); raf.skipBytes(2); raf.skipBytes(2); CentralDirectory dir = new CentralDirectory(); dir.size = Integer.reverseBytes(raf.readInt()) & 0xFFFFFFFFL; dir.offset = Integer.reverseBytes(raf.readInt()) & 0xFFFFFFFFL; return dir; }    }
@Test public void testCrcRange() throws IOException { RandomAccessFile raf = new RandomAccessFile(zipFile, "r"); CentralDirectory dir = ZipUtil.findCentralDirectory(raf); byte[] dirData = new byte[(int) dir.size]; int length = dirData.length; int off = 0; raf.seek(dir.offset); while (length > 0) { int read = raf.read(dirData, off, length); if (length == -1) { throw new EOFException(); } length -= read; off += read; } raf.close(); ByteBuffer buffer = ByteBuffer.wrap(dirData); Map<String, ZipEntry> toCheck = new HashMap<String, ZipEntry>(); while (buffer.hasRemaining()) { buffer = buffer.slice(); buffer.order(ByteOrder.LITTLE_ENDIAN); ZipEntry entry = ZipEntryReader.readEntry(buffer); toCheck.put(entry.getName(), entry); } ZipFile zip = new ZipFile(zipFile); Assert.assertEquals(zip.size(), toCheck.size()); Enumeration<? extends ZipEntry> ref = zip.entries(); while (ref.hasMoreElements()) { ZipEntry refEntry = ref.nextElement(); ZipEntry checkEntry = toCheck.get(refEntry.getName()); Assert.assertNotNull(checkEntry); Assert.assertEquals(refEntry.getName(), checkEntry.getName()); Assert.assertEquals(refEntry.getComment(), checkEntry.getComment()); Assert.assertEquals(refEntry.getTime(), checkEntry.getTime()); Assert.assertEquals(refEntry.getCrc(), checkEntry.getCrc()); Assert.assertEquals(refEntry.getCompressedSize(), checkEntry.getCompressedSize()); Assert.assertEquals(refEntry.getSize(), checkEntry.getSize()); Assert.assertEquals(refEntry.getMethod(), checkEntry.getMethod()); Assert.assertArrayEquals(refEntry.getExtra(), checkEntry.getExtra()); } zip.close(); }