MinimalZipParser { public static long locateStartOfEocd(RandomAccessFileInputStream in, int searchBufferLength) throws IOException { final int maxBufferSize = (int) Math.min(searchBufferLength, in.length()); final byte[] buffer = new byte[maxBufferSize]; final long rangeStart = in.length() - buffer.length; in.setRange(rangeStart, buffer.length); readOrDie(in, buffer, 0, buffer.length); int offset = locateStartOfEocd(buffer); if (offset == -1) { return -1; } return rangeStart + offset; }  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 testLocateStartOfEocd_WithArray() { int eocdOffset = MinimalZipParser.locateStartOfEocd(unitTestZipArchive); checkExpectedBytes(new byte[] {0x50, 0x4b, 0x05, 0x06}, eocdOffset); }
@Test public void testLocateStartOfEocd_WithArray_NoEocd() { int eocdOffset = MinimalZipParser.locateStartOfEocd(new byte[32768]); Assert.assertEquals(-1, eocdOffset); }
@Test public void testLocateStartOfEocd_WithFile() throws IOException { int bytesBefore = 53754; int bytesAfter = 107; File tempFile = File.createTempFile("MinimalZipParserTest", "zip"); tempFile.deleteOnExit(); try { FileOutputStream out = new FileOutputStream(tempFile); out.write(new byte[bytesBefore]); out.write(new byte[] {0x50, 0x4b, 0x05, 0x06}); out.write(new byte[bytesAfter]); out.flush(); out.close(); } catch (IOException e) { try { tempFile.delete(); } catch (Exception ignored) { } throw e; } try (RandomAccessFileInputStream in = new RandomAccessFileInputStream(tempFile)) { long eocdOffset = MinimalZipParser.locateStartOfEocd(in, 32768); Assert.assertEquals(bytesBefore, eocdOffset); } }
@Test public void testLocateStartOfEocd_WithFile_NoEocd() throws IOException { File tempFile = File.createTempFile("MinimalZipParserTest", "zip"); tempFile.deleteOnExit(); try { FileOutputStream out = new FileOutputStream(tempFile); out.write(new byte[4000]); out.flush(); out.close(); } catch (IOException e) { try { tempFile.delete(); } catch (Exception ignored) { } throw e; } try (RandomAccessFileInputStream in = new RandomAccessFileInputStream(tempFile)) { long eocdOffset = MinimalZipParser.locateStartOfEocd(in, 4000); Assert.assertEquals(-1, eocdOffset); } }