@@ -183,15 +183,15 @@ private void closeQuietly(OutputStream outputStream) {
183
183
*/
184
184
private class Processor {
185
185
186
- private ZipArchiveOutputStream out ;
186
+ private final ZipArchiveOutputStream out ;
187
187
188
188
private final LayersIndex layerIndex ;
189
189
190
190
private LoaderZipEntries .WrittenEntries writtenLoaderEntries ;
191
191
192
- private Set <String > writtenDirectories = new LinkedHashSet <>();
192
+ private final Set <String > writtenDirectories = new LinkedHashSet <>();
193
193
194
- private Set <String > writtenLibraries = new LinkedHashSet <>();
194
+ private final Set <String > writtenLibraries = new LinkedHashSet <>();
195
195
196
196
Processor (ZipArchiveOutputStream out ) {
197
197
this .out = out ;
@@ -224,23 +224,17 @@ private boolean skipProcessing(FileCopyDetails details) {
224
224
225
225
private void processDirectory (FileCopyDetails details ) throws IOException {
226
226
String name = details .getRelativePath ().getPathString ();
227
- long time = getTime (details );
228
- writeParentDirectoriesIfNecessary (name , time );
229
227
ZipArchiveEntry entry = new ZipArchiveEntry (name + '/' );
230
- entry .setUnixMode (UnixStat .DIR_FLAG | details .getMode ());
231
- entry .setTime (time );
228
+ prepareEntry (entry , name , getTime (details ), UnixStat .FILE_FLAG | details .getMode ());
232
229
this .out .putArchiveEntry (entry );
233
230
this .out .closeArchiveEntry ();
234
231
this .writtenDirectories .add (name );
235
232
}
236
233
237
234
private void processFile (FileCopyDetails details ) throws IOException {
238
235
String name = details .getRelativePath ().getPathString ();
239
- long time = getTime (details );
240
- writeParentDirectoriesIfNecessary (name , time );
241
236
ZipArchiveEntry entry = new ZipArchiveEntry (name );
242
- entry .setUnixMode (UnixStat .FILE_FLAG | details .getMode ());
243
- entry .setTime (time );
237
+ prepareEntry (entry , name , getTime (details ), UnixStat .FILE_FLAG | details .getMode ());
244
238
ZipCompression compression = BootZipCopyAction .this .compressionResolver .apply (details );
245
239
if (compression == ZipCompression .STORED ) {
246
240
prepareStoredEntry (details , entry );
@@ -257,13 +251,11 @@ private void processFile(FileCopyDetails details) throws IOException {
257
251
}
258
252
}
259
253
260
- private void writeParentDirectoriesIfNecessary (String name , long time ) throws IOException {
254
+ private void writeParentDirectoriesIfNecessary (String name , Long time ) throws IOException {
261
255
String parentDirectory = getParentDirectory (name );
262
256
if (parentDirectory != null && this .writtenDirectories .add (parentDirectory )) {
263
- writeParentDirectoriesIfNecessary (parentDirectory , time );
264
257
ZipArchiveEntry entry = new ZipArchiveEntry (parentDirectory + '/' );
265
- entry .setUnixMode (UnixStat .DIR_FLAG | UnixStat .DEFAULT_DIR_PERM );
266
- entry .setTime (time );
258
+ prepareEntry (entry , parentDirectory , time , UnixStat .DIR_FLAG | UnixStat .DEFAULT_DIR_PERM );
267
259
this .out .putArchiveEntry (entry );
268
260
this .out .closeArchiveEntry ();
269
261
}
@@ -293,8 +285,7 @@ private void writeLoaderEntriesIfNecessary(FileCopyDetails details) throws IOExc
293
285
// Don't write loader entries until after META-INF folder (see gh-16698)
294
286
return ;
295
287
}
296
- LoaderZipEntries loaderEntries = new LoaderZipEntries (
297
- BootZipCopyAction .this .preserveFileTimestamps ? null : CONSTANT_TIME_FOR_ZIP_ENTRIES );
288
+ LoaderZipEntries loaderEntries = new LoaderZipEntries (getTime ());
298
289
this .writtenLoaderEntries = loaderEntries .writeTo (this .out );
299
290
if (BootZipCopyAction .this .layerResolver != null ) {
300
291
for (String name : this .writtenLoaderEntries .getFiles ()) {
@@ -320,37 +311,22 @@ private void writeJarToolsIfNecessary() throws IOException {
320
311
321
312
private void writeJarModeLibrary (String location , JarModeLibrary library ) throws IOException {
322
313
String name = location + library .getName ();
323
- writeEntry (name , ZipEntryWriter .fromInputStream (library .openStream ()), false ,
314
+ writeEntry (name , ZipEntryContentWriter .fromInputStream (library .openStream ()), false ,
324
315
(entry ) -> prepareStoredEntry (library .openStream (), entry ));
325
316
if (BootZipCopyAction .this .layerResolver != null ) {
326
317
Layer layer = BootZipCopyAction .this .layerResolver .getLayer (library );
327
318
this .layerIndex .add (layer , name );
328
319
}
329
320
}
330
321
331
- private void prepareStoredEntry (FileCopyDetails details , ZipArchiveEntry archiveEntry ) throws IOException {
332
- prepareStoredEntry (details .open (), archiveEntry );
333
- if (BootZipCopyAction .this .requiresUnpack .isSatisfiedBy (details )) {
334
- archiveEntry .setComment ("UNPACK:" + FileUtils .sha1Hash (details .getFile ()));
335
- }
336
- }
337
-
338
- private void prepareStoredEntry (InputStream input , ZipArchiveEntry archiveEntry ) throws IOException {
339
- archiveEntry .setMethod (java .util .zip .ZipEntry .STORED );
340
- Crc32OutputStream crcStream = new Crc32OutputStream ();
341
- int size = FileCopyUtils .copy (input , crcStream );
342
- archiveEntry .setSize (size );
343
- archiveEntry .setCompressedSize (size );
344
- archiveEntry .setCrc (crcStream .getCrc ());
345
- }
346
-
347
322
private void writeClassPathIndexIfNecessary () throws IOException {
348
323
Attributes manifestAttributes = BootZipCopyAction .this .manifest .getAttributes ();
349
324
String classPathIndex = (String ) manifestAttributes .get ("Spring-Boot-Classpath-Index" );
350
325
if (classPathIndex != null ) {
351
326
List <String > lines = this .writtenLibraries .stream ().map ((line ) -> "- \" " + line + "\" " )
352
327
.collect (Collectors .toList ());
353
- writeEntry (classPathIndex , ZipEntryWriter .fromLines (BootZipCopyAction .this .encoding , lines ), true );
328
+ writeEntry (classPathIndex , ZipEntryContentWriter .fromLines (BootZipCopyAction .this .encoding , lines ),
329
+ true );
354
330
}
355
331
}
356
332
@@ -361,33 +337,65 @@ private void writeLayersIndexIfNecessary() throws IOException {
361
337
Assert .state (StringUtils .hasText (name ), "Missing layer index manifest attribute" );
362
338
Layer layer = BootZipCopyAction .this .layerResolver .getLayer (name );
363
339
this .layerIndex .add (layer , name );
364
- writeEntry (name , ( entry , out ) -> this .layerIndex . writeTo ( out ) , false );
340
+ writeEntry (name , this .layerIndex :: writeTo , false );
365
341
}
366
342
}
367
343
368
- private void writeEntry (String name , ZipEntryWriter entryWriter , boolean addToLayerIndex ) throws IOException {
344
+ private void writeEntry (String name , ZipEntryContentWriter entryWriter , boolean addToLayerIndex )
345
+ throws IOException {
369
346
writeEntry (name , entryWriter , addToLayerIndex , ZipEntryCustomizer .NONE );
370
347
}
371
348
372
- private void writeEntry (String name , ZipEntryWriter entryWriter , boolean addToLayerIndex ,
349
+ private void writeEntry (String name , ZipEntryContentWriter entryWriter , boolean addToLayerIndex ,
373
350
ZipEntryCustomizer entryCustomizer ) throws IOException {
374
- writeParentDirectoriesIfNecessary (name , CONSTANT_TIME_FOR_ZIP_ENTRIES );
375
351
ZipArchiveEntry entry = new ZipArchiveEntry (name );
376
- entry .setUnixMode (UnixStat .FILE_FLAG | UnixStat .DEFAULT_FILE_PERM );
377
- entry .setTime (CONSTANT_TIME_FOR_ZIP_ENTRIES );
352
+ prepareEntry (entry , name , getTime (), UnixStat .FILE_FLAG | UnixStat .DEFAULT_FILE_PERM );
378
353
entryCustomizer .customize (entry );
379
354
this .out .putArchiveEntry (entry );
380
- entryWriter .writeTo (entry , this .out );
355
+ entryWriter .writeTo (this .out );
381
356
this .out .closeArchiveEntry ();
382
357
if (addToLayerIndex && BootZipCopyAction .this .layerResolver != null ) {
383
358
Layer layer = BootZipCopyAction .this .layerResolver .getLayer (name );
384
359
this .layerIndex .add (layer , name );
385
360
}
386
361
}
387
362
388
- private long getTime (FileCopyDetails details ) {
389
- return BootZipCopyAction .this .preserveFileTimestamps ? details .getLastModified ()
390
- : CONSTANT_TIME_FOR_ZIP_ENTRIES ;
363
+ private void prepareEntry (ZipArchiveEntry entry , String name , Long time , int mode ) throws IOException {
364
+ writeParentDirectoriesIfNecessary (name , time );
365
+ entry .setUnixMode (mode );
366
+ if (time != null ) {
367
+ entry .setTime (time );
368
+ }
369
+ }
370
+
371
+ private void prepareStoredEntry (FileCopyDetails details , ZipArchiveEntry archiveEntry ) throws IOException {
372
+ prepareStoredEntry (details .open (), archiveEntry );
373
+ if (BootZipCopyAction .this .requiresUnpack .isSatisfiedBy (details )) {
374
+ archiveEntry .setComment ("UNPACK:" + FileUtils .sha1Hash (details .getFile ()));
375
+ }
376
+ }
377
+
378
+ private void prepareStoredEntry (InputStream input , ZipArchiveEntry archiveEntry ) throws IOException {
379
+ archiveEntry .setMethod (java .util .zip .ZipEntry .STORED );
380
+ Crc32OutputStream crcStream = new Crc32OutputStream ();
381
+ int size = FileCopyUtils .copy (input , crcStream );
382
+ archiveEntry .setSize (size );
383
+ archiveEntry .setCompressedSize (size );
384
+ archiveEntry .setCrc (crcStream .getCrc ());
385
+ }
386
+
387
+ private Long getTime () {
388
+ return getTime (null );
389
+ }
390
+
391
+ private Long getTime (FileCopyDetails details ) {
392
+ if (!BootZipCopyAction .this .preserveFileTimestamps ) {
393
+ return CONSTANT_TIME_FOR_ZIP_ENTRIES ;
394
+ }
395
+ if (details != null ) {
396
+ return details .getLastModified ();
397
+ }
398
+ return null ;
391
399
}
392
400
393
401
}
@@ -414,41 +422,40 @@ private interface ZipEntryCustomizer {
414
422
* Callback used to write a zip entry data.
415
423
*/
416
424
@ FunctionalInterface
417
- private interface ZipEntryWriter {
425
+ private interface ZipEntryContentWriter {
418
426
419
427
/**
420
428
* Write the entry data.
421
- * @param entry the entry being written
422
429
* @param out the output stream used to write the data
423
430
* @throws IOException on IO error
424
431
*/
425
- void writeTo (ZipArchiveEntry entry , ZipArchiveOutputStream out ) throws IOException ;
432
+ void writeTo (ZipArchiveOutputStream out ) throws IOException ;
426
433
427
434
/**
428
- * Create a new {@link ZipEntryWriter } that will copy content from the given
429
- * {@link InputStream}.
435
+ * Create a new {@link ZipEntryContentWriter } that will copy content from the
436
+ * given {@link InputStream}.
430
437
* @param in the source input stream
431
- * @return a new {@link ZipEntryWriter } instance
438
+ * @return a new {@link ZipEntryContentWriter } instance
432
439
*/
433
- static ZipEntryWriter fromInputStream (InputStream in ) {
434
- return (entry , out ) -> {
440
+ static ZipEntryContentWriter fromInputStream (InputStream in ) {
441
+ return (out ) -> {
435
442
StreamUtils .copy (in , out );
436
443
in .close ();
437
444
};
438
445
}
439
446
440
447
/**
441
- * Create a new {@link ZipEntryWriter } that will copy content from the given
442
- * lines.
448
+ * Create a new {@link ZipEntryContentWriter } that will copy content from the
449
+ * given lines.
443
450
* @param encoding the required character encoding
444
451
* @param lines the lines to write
445
- * @return a new {@link ZipEntryWriter } instance
452
+ * @return a new {@link ZipEntryContentWriter } instance
446
453
*/
447
- static ZipEntryWriter fromLines (String encoding , Collection <String > lines ) {
448
- return (entry , out ) -> {
454
+ static ZipEntryContentWriter fromLines (String encoding , Collection <String > lines ) {
455
+ return (out ) -> {
449
456
OutputStreamWriter writer = new OutputStreamWriter (out , encoding );
450
457
for (String line : lines ) {
451
- writer .append (line + "\n " );
458
+ writer .append (line ). append ( "\n " );
452
459
}
453
460
writer .flush ();
454
461
};
0 commit comments