MinimalZipParser { public static long parseLocalEntryAndGetCompressedDataOffset(InputStream in) throws IOException { if (((int) read32BitUnsigned(in)) != LOCAL_ENTRY_SIGNATURE) { throw new ZipException("Bad local entry header"); } int junkLength = 2 + 2 + 2 + 2 + 2 + 4 + 4 + 4; skipOrDie(in, junkLength); final int fileNameLength = read16BitUnsigned(in); final int extrasLength = read16BitUnsigned(in); return 4 + junkLength + 2 + 2 + fileNameLength + extrasLength; }  static MinimalZipEntry parseCentralDirectoryEntry(InputStream in); static long parseLocalEntryAndGetCompressedDataOffset(InputStream in); static long locateStartOfEocd(RandomAccessFileInputStream in, int searchBufferLength); static int locateStartOfEocd(byte[] buffer); static MinimalCentralDirectoryMetadata parseEocd(InputStream in); static final int EOCD_SIGNATURE; static final int CENTRAL_DIRECTORY_ENTRY_SIGNATURE; static final int LOCAL_ENTRY_SIGNATURE; }
@Test public void testParseLocalEntryAndGetCompressedDataOffset() throws Exception { ByteArrayInputStream in = new ByteArrayInputStream(unitTestZipArchive); in.mark(unitTestZipArchive.length); int eocdOffset = MinimalZipParser.locateStartOfEocd(unitTestZipArchive); Assert.assertEquals(eocdOffset, in.skip(eocdOffset)); MinimalCentralDirectoryMetadata metadata = MinimalZipParser.parseEocd(in); in.reset(); Assert.assertEquals( metadata.getOffsetOfCentralDirectory(), in.skip(metadata.getOffsetOfCentralDirectory())); List<MinimalZipEntry> parsedEntries = new ArrayList<MinimalZipEntry>(); for (int x = 0; x < UnitTestZipArchive.allEntriesInFileOrder.size(); x++) { parsedEntries.add(MinimalZipParser.parseCentralDirectoryEntry(in)); } for (int x = 0; x < UnitTestZipArchive.allEntriesInFileOrder.size(); x++) { UnitTestZipEntry expectedEntry = UnitTestZipArchive.allEntriesInFileOrder.get(x); MinimalZipEntry parsedEntry = parsedEntries.get(x); in.reset(); Assert.assertEquals( parsedEntry.getFileOffsetOfLocalEntry(), in.skip(parsedEntry.getFileOffsetOfLocalEntry())); long relativeDataOffset = MinimalZipParser.parseLocalEntryAndGetCompressedDataOffset(in); Assert.assertTrue(relativeDataOffset > 0); checkExpectedBytes( expectedEntry.getCompressedBinaryContent(), (int) (parsedEntry.getFileOffsetOfLocalEntry() + relativeDataOffset)); } }