Polish "Support zip64 jars"

See gh-16091
This commit is contained in:
Andy Wilkinson
2019-09-20 12:42:30 +01:00
parent 1917e1eac5
commit 02ac089767
4 changed files with 115 additions and 54 deletions

View File

@@ -17,7 +17,6 @@
package org.springframework.boot.loader.jar;
import java.io.IOException;
import java.util.Optional;
import org.springframework.boot.loader.data.RandomAccessData;
@@ -45,7 +44,7 @@ class CentralDirectoryEndRecord {
private static final int READ_BLOCK_SIZE = 256;
private final Optional<Zip64End> zip64End;
private final Zip64End zip64End;
private byte[] block;
@@ -76,8 +75,7 @@ class CentralDirectoryEndRecord {
this.offset = this.block.length - this.size;
}
int startOfCentralDirectoryEndRecord = (int) (data.getSize() - this.size);
this.zip64End = Optional.ofNullable(
isZip64() ? new Zip64End(data, startOfCentralDirectoryEndRecord) : null);
this.zip64End = isZip64() ? new Zip64End(data, startOfCentralDirectoryEndRecord) : null;
}
private byte[] createBlockFromEndOfData(RandomAccessData data, int size) throws IOException {
@@ -94,6 +92,10 @@ class CentralDirectoryEndRecord {
return this.size == MINIMUM_SIZE + commentLength;
}
private boolean isZip64() {
return (int) Bytes.littleEndianValue(this.block, this.offset + 10, 2) == ZIP64_MAGICCOUNT;
}
/**
* Returns the location in the data that the archive actually starts. For most files
* the archive data will start at 0, however, it is possible to have prefixed bytes
@@ -104,10 +106,9 @@ class CentralDirectoryEndRecord {
long getStartOfArchive(RandomAccessData data) {
long length = Bytes.littleEndianValue(this.block, this.offset + 12, 4);
long specifiedOffset = Bytes.littleEndianValue(this.block, this.offset + 16, 4);
long zip64EndSize = this.zip64End.map((x) -> x.getSize()).orElse(0L);
int zip64LocSize = this.zip64End.map((x) -> Zip64Locator.ZIP64_LOCSIZE).orElse(0);
long actualOffset = data.getSize() - this.size - length - zip64EndSize
- zip64LocSize;
long zip64EndSize = (this.zip64End != null) ? this.zip64End.getSize() : 0L;
int zip64LocSize = (this.zip64End != null) ? Zip64Locator.ZIP64_LOCSIZE : 0;
long actualOffset = data.getSize() - this.size - length - zip64EndSize - zip64LocSize;
return actualOffset - specifiedOffset;
}
@@ -118,14 +119,12 @@ class CentralDirectoryEndRecord {
* @return the central directory data
*/
RandomAccessData getCentralDirectory(RandomAccessData data) {
if (isZip64()) {
return this.zip64End.get().getCentratDirectory(data);
}
else {
long offset = Bytes.littleEndianValue(this.block, this.offset + 16, 4);
long length = Bytes.littleEndianValue(this.block, this.offset + 12, 4);
return data.getSubsection(offset, length);
if (this.zip64End != null) {
return this.zip64End.getCentralDirectory(data);
}
long offset = Bytes.littleEndianValue(this.block, this.offset + 16, 4);
long length = Bytes.littleEndianValue(this.block, this.offset + 12, 4);
return data.getSubsection(offset, length);
}
/**
@@ -133,19 +132,17 @@ class CentralDirectoryEndRecord {
* @return the number of records in the zip
*/
int getNumberOfRecords() {
if (isZip64()) {
return this.zip64End.get().getNumberOfRecords();
}
else {
long numberOfRecords = Bytes.littleEndianValue(this.block, this.offset + 10,
2);
return (int) numberOfRecords;
if (this.zip64End != null) {
return this.zip64End.getNumberOfRecords();
}
long numberOfRecords = Bytes.littleEndianValue(this.block, this.offset + 10, 2);
return (int) numberOfRecords;
}
boolean isZip64() {
return (int) Bytes.littleEndianValue(this.block, this.offset + 10,
2) == ZIP64_MAGICCOUNT;
String getComment() {
int commentLength = (int) Bytes.littleEndianValue(this.block, this.offset + COMMENT_LENGTH_OFFSET, 2);
AsciiBytes comment = new AsciiBytes(this.block, this.offset + COMMENT_LENGTH_OFFSET + 2, commentLength);
return comment.toString();
}
/**
@@ -154,11 +151,13 @@ class CentralDirectoryEndRecord {
* @see <a href="https://pkware.cachefly.net/webdocs/casestudies/APPNOTE.TXT">Chapter
* 4.3.14 of Zip64 specification</a>
*/
private static class Zip64End {
private static final class Zip64End {
static final int ZIP64_ENDTOT = 32; // total number of entries
static final int ZIP64_ENDSIZ = 40; // central directory size in bytes
static final int ZIP64_ENDOFF = 48; // offset of first CEN header
private static final int ZIP64_ENDTOT = 32; // total number of entries
private static final int ZIP64_ENDSIZ = 40; // central directory size in bytes
private static final int ZIP64_ENDOFF = 48; // offset of first CEN header
private final Zip64Locator locator;
@@ -168,12 +167,11 @@ class CentralDirectoryEndRecord {
private int numberOfRecords;
Zip64End(RandomAccessData data, int centratDirectoryEndOffset)
throws IOException {
private Zip64End(RandomAccessData data, int centratDirectoryEndOffset) throws IOException {
this(data, new Zip64Locator(data, centratDirectoryEndOffset));
}
Zip64End(RandomAccessData data, Zip64Locator locator) throws IOException {
private Zip64End(RandomAccessData data, Zip64Locator locator) throws IOException {
this.locator = locator;
byte[] block = data.read(locator.getZip64EndOffset(), 56);
this.centralDirectoryOffset = Bytes.littleEndianValue(block, ZIP64_ENDOFF, 8);
@@ -185,7 +183,7 @@ class CentralDirectoryEndRecord {
* Return the size of this zip 64 end of central directory record.
* @return size of this zip 64 end of central directory record
*/
public long getSize() {
private long getSize() {
return this.locator.getZip64EndSize();
}
@@ -195,16 +193,15 @@ class CentralDirectoryEndRecord {
* @param data the source data
* @return the central directory data
*/
public RandomAccessData getCentratDirectory(RandomAccessData data) {
return data.getSubsection(this.centralDirectoryOffset,
this.centralDirectoryLength);
private RandomAccessData getCentralDirectory(RandomAccessData data) {
return data.getSubsection(this.centralDirectoryOffset, this.centralDirectoryLength);
}
/**
* Return the number of entries in the zip64 archive.
* @return the number of records in the zip
*/
public int getNumberOfRecords() {
private int getNumberOfRecords() {
return this.numberOfRecords;
}
@@ -216,7 +213,7 @@ class CentralDirectoryEndRecord {
* @see <a href="https://pkware.cachefly.net/webdocs/casestudies/APPNOTE.TXT">Chapter
* 4.3.15 of Zip64 specification</a>
*/
private static class Zip64Locator {
private static final class Zip64Locator {
static final int ZIP64_LOCSIZE = 20; // locator size
static final int ZIP64_LOCOFF = 8; // offset of zip64 end
@@ -225,8 +222,7 @@ class CentralDirectoryEndRecord {
private final int offset;
Zip64Locator(RandomAccessData data, int centralDirectoryEndOffset)
throws IOException {
private Zip64Locator(RandomAccessData data, int centralDirectoryEndOffset) throws IOException {
this.offset = centralDirectoryEndOffset - ZIP64_LOCSIZE;
byte[] block = data.read(this.offset, ZIP64_LOCSIZE);
this.zip64EndOffset = Bytes.littleEndianValue(block, ZIP64_LOCOFF, 8);
@@ -236,7 +232,7 @@ class CentralDirectoryEndRecord {
* Return the size of the zip 64 end record located by this zip64 end locator.
* @return size of the zip 64 end record located by this zip64 end locator
*/
public long getZip64EndSize() {
private long getZip64EndSize() {
return this.offset - this.zip64EndOffset;
}
@@ -244,16 +240,10 @@ class CentralDirectoryEndRecord {
* Return the offset to locate {@link Zip64End}.
* @return offset of the Zip64 end of central directory record
*/
public long getZip64EndOffset() {
private long getZip64EndOffset() {
return this.zip64EndOffset;
}
}
String getComment() {
int commentLength = (int) Bytes.littleEndianValue(this.block, this.offset + COMMENT_LENGTH_OFFSET, 2);
AsciiBytes comment = new AsciiBytes(this.block, this.offset + COMMENT_LENGTH_OFFSET + 2, commentLength);
return comment.toString();
}
}