21
21
import org .apache .commons .compress .archivers .zip .ZipArchiveOutputStream ;
22
22
import org .apache .commons .compress .archivers .zip .ZipEncoding ;
23
23
import org .apache .commons .compress .archivers .zip .ZipEncodingHelper ;
24
- import java .util .concurrent .ExecutionException ;
25
- import java .util .zip .CRC32 ;
26
-
27
24
import org .apache .commons .compress .parallel .InputStreamSupplier ;
28
25
import org .codehaus .plexus .archiver .AbstractArchiver ;
29
26
import org .codehaus .plexus .archiver .ArchiveEntry ;
47
44
import java .io .SequenceInputStream ;
48
45
import java .util .Hashtable ;
49
46
import java .util .Stack ;
47
+ import java .util .concurrent .ExecutionException ;
48
+ import java .util .zip .CRC32 ;
50
49
51
50
import static org .codehaus .plexus .archiver .util .Streams .bufferedOutputStream ;
52
51
import static org .codehaus .plexus .archiver .util .Streams .fileOutputStream ;
@@ -82,7 +81,7 @@ public abstract class AbstractZipArchiver
82
81
83
82
protected final Hashtable <String , String > entries = new Hashtable <String , String >();
84
83
85
- protected final AddedDirs addedDirs = new AddedDirs ();
84
+ protected final AddedDirs addedDirs = new AddedDirs ();
86
85
87
86
private static final long EMPTY_CRC = new CRC32 ().getValue ();
88
87
@@ -115,7 +114,8 @@ public abstract class AbstractZipArchiver
115
114
* <p/>
116
115
* plexus-archiver chooses to round up.
117
116
* <p/>
118
- * Java versions up to java7 round timestamp down, which means we add a heuristic value (which is slightly questionable)
117
+ * Java versions up to java7 round timestamp down, which means we add a heuristic value (which is slightly
118
+ * questionable)
119
119
* Java versions from 8 and up round timestamp up.
120
120
* s
121
121
*/
@@ -407,16 +407,16 @@ private void addParentDirs(ArchiveEntry archiveEntry, File baseDir, String entry
407
407
408
408
/**
409
409
* Adds a new entry to the archive, takes care of duplicates as well.
410
- *
411
- * @param in the stream to read data for the entry from.
410
+ * @param in the stream to read data for the entry from.
412
411
* @param zOut the stream to write to.
413
412
* @param vPath the name this entry shall have in the archive.
414
413
* @param lastModified last modification time for the entry.
415
414
* @param fromArchive the original archive we are copying this
416
415
* @param symlinkDestination
417
416
*/
418
417
@ SuppressWarnings ( { "JavaDoc" } )
419
- protected void zipFile ( @ WillClose InputStream in , ConcurrentJarCreator zOut , String vPath , long lastModified ,
418
+ protected void zipFile ( InputStreamSupplier in , ConcurrentJarCreator zOut , String vPath ,
419
+ long lastModified ,
420
420
File fromArchive , int mode , String symlinkDestination )
421
421
throws IOException , ArchiverException
422
422
{
@@ -429,16 +429,7 @@ protected void zipFile( @WillClose InputStream in, ConcurrentJarCreator zOut, St
429
429
ZipArchiveEntry ze = new ZipArchiveEntry ( vPath );
430
430
setTime ( ze , lastModified );
431
431
432
- byte [] header = new byte [4 ];
433
- int read = in .read ( header );
434
-
435
- boolean compressThis = doCompress ;
436
- if ( !recompressAddedZips && isZipHeader ( header ) )
437
- {
438
- compressThis = false ;
439
- }
440
-
441
- ze .setMethod ( compressThis ? ZipArchiveEntry .DEFLATED : ZipArchiveEntry .STORED );
432
+ ze .setMethod ( doCompress ? ZipArchiveEntry .DEFLATED : ZipArchiveEntry .STORED );
442
433
ze .setUnixMode ( UnixStat .FILE_FLAG | mode );
443
434
444
435
InputStream payload ;
@@ -447,13 +438,12 @@ protected void zipFile( @WillClose InputStream in, ConcurrentJarCreator zOut, St
447
438
ZipEncoding enc = ZipEncodingHelper .getZipEncoding ( getEncoding () );
448
439
final byte [] bytes = enc .encode ( symlinkDestination ).array ();
449
440
payload = new ByteArrayInputStream ( bytes );
441
+ zOut .addArchiveEntry ( ze , createInputStreamSupplier ( payload ) );
450
442
}
451
443
else
452
444
{
453
- payload = maybeSequence ( header , read , in );
445
+ zOut . addArchiveEntry ( ze , wrappedRecompressor ( ze , in ) );
454
446
}
455
- zOut .addArchiveEntry ( ze , createInputStreamSupplier ( payload ) );
456
-
457
447
}
458
448
}
459
449
@@ -476,7 +466,7 @@ private boolean isZipHeader( byte[] header )
476
466
* @param vPath the name this entry shall have in the archive
477
467
*/
478
468
@ SuppressWarnings ( { "JavaDoc" } )
479
- protected void zipFile ( ArchiveEntry entry , ConcurrentJarCreator zOut , String vPath )
469
+ protected void zipFile ( final ArchiveEntry entry , ConcurrentJarCreator zOut , String vPath )
480
470
throws IOException , ArchiverException
481
471
{
482
472
final PlexusIoResource resource = entry .getResource ();
@@ -487,7 +477,21 @@ protected void zipFile( ArchiveEntry entry, ConcurrentJarCreator zOut, String vP
487
477
488
478
final boolean b = entry .getResource () instanceof SymlinkDestinationSupplier ;
489
479
String symlinkTarget = b ? ( (SymlinkDestinationSupplier ) entry .getResource () ).getSymlinkDestination () : null ;
490
- InputStream in = entry .getInputStream ();
480
+ InputStreamSupplier in = new InputStreamSupplier ()
481
+ {
482
+ @ Override
483
+ public InputStream get ()
484
+ {
485
+ try
486
+ {
487
+ return entry .getInputStream ();
488
+ }
489
+ catch ( IOException e )
490
+ {
491
+ throw new RuntimeException ( e );
492
+ }
493
+ }
494
+ };
491
495
try
492
496
{
493
497
zipFile ( in , zOut , vPath , resource .getLastModified (), null , entry .getMode (), symlinkTarget );
@@ -584,7 +588,39 @@ protected void zipDir( PlexusIoResource dir, ConcurrentJarCreator zOut, String v
584
588
}
585
589
}
586
590
587
- private InputStreamSupplier createInputStreamSupplier ( final InputStream inputStream )
591
+
592
+ private InputStreamSupplier wrappedRecompressor ( final ZipArchiveEntry ze , final InputStreamSupplier other )
593
+ {
594
+
595
+ return new InputStreamSupplier ()
596
+ {
597
+ public InputStream get ()
598
+ {
599
+ InputStream is = other .get ();
600
+ byte [] header = new byte [4 ];
601
+ try
602
+ {
603
+ int read = is .read ( header );
604
+ boolean compressThis = doCompress ;
605
+ if ( !recompressAddedZips && isZipHeader ( header ) )
606
+ {
607
+ compressThis = false ;
608
+ }
609
+
610
+ ze .setMethod ( compressThis ? ZipArchiveEntry .DEFLATED : ZipArchiveEntry .STORED );
611
+
612
+ return maybeSequence ( header , read , is );
613
+ }
614
+ catch ( IOException e )
615
+ {
616
+ throw new RuntimeException ( e );
617
+ }
618
+
619
+ }
620
+ };
621
+ }
622
+
623
+ protected InputStreamSupplier createInputStreamSupplier ( final InputStream inputStream )
588
624
{
589
625
return new InputStreamSupplier ()
590
626
{
0 commit comments