1717package org .springframework .boot .loader .jar ;
1818
1919import java .io .IOException ;
20- import java .util .Optional ;
2120
2221import org .springframework .boot .loader .data .RandomAccessData ;
2322
@@ -45,7 +44,7 @@ class CentralDirectoryEndRecord {
4544
4645 private static final int READ_BLOCK_SIZE = 256 ;
4746
48- private final Optional < Zip64End > zip64End ;
47+ private final Zip64End zip64End ;
4948
5049 private byte [] block ;
5150
@@ -76,8 +75,7 @@ class CentralDirectoryEndRecord {
7675 this .offset = this .block .length - this .size ;
7776 }
7877 int startOfCentralDirectoryEndRecord = (int ) (data .getSize () - this .size );
79- this .zip64End = Optional .ofNullable (
80- isZip64 () ? new Zip64End (data , startOfCentralDirectoryEndRecord ) : null );
78+ this .zip64End = isZip64 () ? new Zip64End (data , startOfCentralDirectoryEndRecord ) : null ;
8179 }
8280
8381 private byte [] createBlockFromEndOfData (RandomAccessData data , int size ) throws IOException {
@@ -94,6 +92,10 @@ private boolean isValid() {
9492 return this .size == MINIMUM_SIZE + commentLength ;
9593 }
9694
95+ private boolean isZip64 () {
96+ return (int ) Bytes .littleEndianValue (this .block , this .offset + 10 , 2 ) == ZIP64_MAGICCOUNT ;
97+ }
98+
9799 /**
98100 * Returns the location in the data that the archive actually starts. For most files
99101 * the archive data will start at 0, however, it is possible to have prefixed bytes
@@ -104,10 +106,9 @@ private boolean isValid() {
104106 long getStartOfArchive (RandomAccessData data ) {
105107 long length = Bytes .littleEndianValue (this .block , this .offset + 12 , 4 );
106108 long specifiedOffset = Bytes .littleEndianValue (this .block , this .offset + 16 , 4 );
107- long zip64EndSize = this .zip64End .map ((x ) -> x .getSize ()).orElse (0L );
108- int zip64LocSize = this .zip64End .map ((x ) -> Zip64Locator .ZIP64_LOCSIZE ).orElse (0 );
109- long actualOffset = data .getSize () - this .size - length - zip64EndSize
110- - zip64LocSize ;
109+ long zip64EndSize = (this .zip64End != null ) ? this .zip64End .getSize () : 0L ;
110+ int zip64LocSize = (this .zip64End != null ) ? Zip64Locator .ZIP64_LOCSIZE : 0 ;
111+ long actualOffset = data .getSize () - this .size - length - zip64EndSize - zip64LocSize ;
111112 return actualOffset - specifiedOffset ;
112113 }
113114
@@ -118,34 +119,30 @@ long getStartOfArchive(RandomAccessData data) {
118119 * @return the central directory data
119120 */
120121 RandomAccessData getCentralDirectory (RandomAccessData data ) {
121- if (isZip64 ()) {
122- return this .zip64End .get ().getCentratDirectory (data );
123- }
124- else {
125- long offset = Bytes .littleEndianValue (this .block , this .offset + 16 , 4 );
126- long length = Bytes .littleEndianValue (this .block , this .offset + 12 , 4 );
127- return data .getSubsection (offset , length );
122+ if (this .zip64End != null ) {
123+ return this .zip64End .getCentralDirectory (data );
128124 }
125+ long offset = Bytes .littleEndianValue (this .block , this .offset + 16 , 4 );
126+ long length = Bytes .littleEndianValue (this .block , this .offset + 12 , 4 );
127+ return data .getSubsection (offset , length );
129128 }
130129
131130 /**
132131 * Return the number of ZIP entries in the file.
133132 * @return the number of records in the zip
134133 */
135134 int getNumberOfRecords () {
136- if (isZip64 ()) {
137- return this .zip64End .get ().getNumberOfRecords ();
138- }
139- else {
140- long numberOfRecords = Bytes .littleEndianValue (this .block , this .offset + 10 ,
141- 2 );
142- return (int ) numberOfRecords ;
135+ if (this .zip64End != null ) {
136+ return this .zip64End .getNumberOfRecords ();
143137 }
138+ long numberOfRecords = Bytes .littleEndianValue (this .block , this .offset + 10 , 2 );
139+ return (int ) numberOfRecords ;
144140 }
145141
146- boolean isZip64 () {
147- return (int ) Bytes .littleEndianValue (this .block , this .offset + 10 ,
148- 2 ) == ZIP64_MAGICCOUNT ;
142+ String getComment () {
143+ int commentLength = (int ) Bytes .littleEndianValue (this .block , this .offset + COMMENT_LENGTH_OFFSET , 2 );
144+ AsciiBytes comment = new AsciiBytes (this .block , this .offset + COMMENT_LENGTH_OFFSET + 2 , commentLength );
145+ return comment .toString ();
149146 }
150147
151148 /**
@@ -154,11 +151,13 @@ boolean isZip64() {
154151 * @see <a href="https://pkware.cachefly.net/webdocs/casestudies/APPNOTE.TXT">Chapter
155152 * 4.3.14 of Zip64 specification</a>
156153 */
157- private static class Zip64End {
154+ private static final class Zip64End {
158155
159- static final int ZIP64_ENDTOT = 32 ; // total number of entries
160- static final int ZIP64_ENDSIZ = 40 ; // central directory size in bytes
161- static final int ZIP64_ENDOFF = 48 ; // offset of first CEN header
156+ private static final int ZIP64_ENDTOT = 32 ; // total number of entries
157+
158+ private static final int ZIP64_ENDSIZ = 40 ; // central directory size in bytes
159+
160+ private static final int ZIP64_ENDOFF = 48 ; // offset of first CEN header
162161
163162 private final Zip64Locator locator ;
164163
@@ -168,12 +167,11 @@ private static class Zip64End {
168167
169168 private int numberOfRecords ;
170169
171- Zip64End (RandomAccessData data , int centratDirectoryEndOffset )
172- throws IOException {
170+ private Zip64End (RandomAccessData data , int centratDirectoryEndOffset ) throws IOException {
173171 this (data , new Zip64Locator (data , centratDirectoryEndOffset ));
174172 }
175173
176- Zip64End (RandomAccessData data , Zip64Locator locator ) throws IOException {
174+ private Zip64End (RandomAccessData data , Zip64Locator locator ) throws IOException {
177175 this .locator = locator ;
178176 byte [] block = data .read (locator .getZip64EndOffset (), 56 );
179177 this .centralDirectoryOffset = Bytes .littleEndianValue (block , ZIP64_ENDOFF , 8 );
@@ -185,7 +183,7 @@ private static class Zip64End {
185183 * Return the size of this zip 64 end of central directory record.
186184 * @return size of this zip 64 end of central directory record
187185 */
188- public long getSize () {
186+ private long getSize () {
189187 return this .locator .getZip64EndSize ();
190188 }
191189
@@ -195,16 +193,15 @@ public long getSize() {
195193 * @param data the source data
196194 * @return the central directory data
197195 */
198- public RandomAccessData getCentratDirectory (RandomAccessData data ) {
199- return data .getSubsection (this .centralDirectoryOffset ,
200- this .centralDirectoryLength );
196+ private RandomAccessData getCentralDirectory (RandomAccessData data ) {
197+ return data .getSubsection (this .centralDirectoryOffset , this .centralDirectoryLength );
201198 }
202199
203200 /**
204201 * Return the number of entries in the zip64 archive.
205202 * @return the number of records in the zip
206203 */
207- public int getNumberOfRecords () {
204+ private int getNumberOfRecords () {
208205 return this .numberOfRecords ;
209206 }
210207
@@ -216,7 +213,7 @@ public int getNumberOfRecords() {
216213 * @see <a href="https://pkware.cachefly.net/webdocs/casestudies/APPNOTE.TXT">Chapter
217214 * 4.3.15 of Zip64 specification</a>
218215 */
219- private static class Zip64Locator {
216+ private static final class Zip64Locator {
220217
221218 static final int ZIP64_LOCSIZE = 20 ; // locator size
222219 static final int ZIP64_LOCOFF = 8 ; // offset of zip64 end
@@ -225,8 +222,7 @@ private static class Zip64Locator {
225222
226223 private final int offset ;
227224
228- Zip64Locator (RandomAccessData data , int centralDirectoryEndOffset )
229- throws IOException {
225+ private Zip64Locator (RandomAccessData data , int centralDirectoryEndOffset ) throws IOException {
230226 this .offset = centralDirectoryEndOffset - ZIP64_LOCSIZE ;
231227 byte [] block = data .read (this .offset , ZIP64_LOCSIZE );
232228 this .zip64EndOffset = Bytes .littleEndianValue (block , ZIP64_LOCOFF , 8 );
@@ -236,24 +232,18 @@ private static class Zip64Locator {
236232 * Return the size of the zip 64 end record located by this zip64 end locator.
237233 * @return size of the zip 64 end record located by this zip64 end locator
238234 */
239- public long getZip64EndSize () {
235+ private long getZip64EndSize () {
240236 return this .offset - this .zip64EndOffset ;
241237 }
242238
243239 /**
244240 * Return the offset to locate {@link Zip64End}.
245241 * @return offset of the Zip64 end of central directory record
246242 */
247- public long getZip64EndOffset () {
243+ private long getZip64EndOffset () {
248244 return this .zip64EndOffset ;
249245 }
250246
251247 }
252248
253- String getComment () {
254- int commentLength = (int ) Bytes .littleEndianValue (this .block , this .offset + COMMENT_LENGTH_OFFSET , 2 );
255- AsciiBytes comment = new AsciiBytes (this .block , this .offset + COMMENT_LENGTH_OFFSET + 2 , commentLength );
256- return comment .toString ();
257- }
258-
259249}
0 commit comments