2121import org .apache .commons .compress .archivers .zip .ZipArchiveOutputStream ;
2222import org .apache .commons .compress .archivers .zip .ZipEncoding ;
2323import org .apache .commons .compress .archivers .zip .ZipEncodingHelper ;
24- import java .util .concurrent .ExecutionException ;
25- import java .util .zip .CRC32 ;
26-
2724import org .apache .commons .compress .parallel .InputStreamSupplier ;
2825import org .codehaus .plexus .archiver .AbstractArchiver ;
2926import org .codehaus .plexus .archiver .ArchiveEntry ;
4744import java .io .SequenceInputStream ;
4845import java .util .Hashtable ;
4946import java .util .Stack ;
47+ import java .util .concurrent .ExecutionException ;
48+ import java .util .zip .CRC32 ;
5049
5150import static org .codehaus .plexus .archiver .util .Streams .bufferedOutputStream ;
5251import static org .codehaus .plexus .archiver .util .Streams .fileOutputStream ;
@@ -82,7 +81,7 @@ public abstract class AbstractZipArchiver
8281
8382 protected final Hashtable <String , String > entries = new Hashtable <String , String >();
8483
85- protected final AddedDirs addedDirs = new AddedDirs ();
84+ protected final AddedDirs addedDirs = new AddedDirs ();
8685
8786 private static final long EMPTY_CRC = new CRC32 ().getValue ();
8887
@@ -115,7 +114,8 @@ public abstract class AbstractZipArchiver
115114 * <p/>
116115 * plexus-archiver chooses to round up.
117116 * <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)
119119 * Java versions from 8 and up round timestamp up.
120120 * s
121121 */
@@ -407,16 +407,16 @@ private void addParentDirs(ArchiveEntry archiveEntry, File baseDir, String entry
407407
408408 /**
409409 * 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.
412411 * @param zOut the stream to write to.
413412 * @param vPath the name this entry shall have in the archive.
414413 * @param lastModified last modification time for the entry.
415414 * @param fromArchive the original archive we are copying this
416415 * @param symlinkDestination
417416 */
418417 @ 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 ,
420420 File fromArchive , int mode , String symlinkDestination )
421421 throws IOException , ArchiverException
422422 {
@@ -429,16 +429,7 @@ protected void zipFile( @WillClose InputStream in, ConcurrentJarCreator zOut, St
429429 ZipArchiveEntry ze = new ZipArchiveEntry ( vPath );
430430 setTime ( ze , lastModified );
431431
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 );
442433 ze .setUnixMode ( UnixStat .FILE_FLAG | mode );
443434
444435 InputStream payload ;
@@ -447,13 +438,12 @@ protected void zipFile( @WillClose InputStream in, ConcurrentJarCreator zOut, St
447438 ZipEncoding enc = ZipEncodingHelper .getZipEncoding ( getEncoding () );
448439 final byte [] bytes = enc .encode ( symlinkDestination ).array ();
449440 payload = new ByteArrayInputStream ( bytes );
441+ zOut .addArchiveEntry ( ze , createInputStreamSupplier ( payload ) );
450442 }
451443 else
452444 {
453- payload = maybeSequence ( header , read , in );
445+ zOut . addArchiveEntry ( ze , wrappedRecompressor ( ze , in ) );
454446 }
455- zOut .addArchiveEntry ( ze , createInputStreamSupplier ( payload ) );
456-
457447 }
458448 }
459449
@@ -476,7 +466,7 @@ private boolean isZipHeader( byte[] header )
476466 * @param vPath the name this entry shall have in the archive
477467 */
478468 @ SuppressWarnings ( { "JavaDoc" } )
479- protected void zipFile ( ArchiveEntry entry , ConcurrentJarCreator zOut , String vPath )
469+ protected void zipFile ( final ArchiveEntry entry , ConcurrentJarCreator zOut , String vPath )
480470 throws IOException , ArchiverException
481471 {
482472 final PlexusIoResource resource = entry .getResource ();
@@ -487,7 +477,21 @@ protected void zipFile( ArchiveEntry entry, ConcurrentJarCreator zOut, String vP
487477
488478 final boolean b = entry .getResource () instanceof SymlinkDestinationSupplier ;
489479 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+ };
491495 try
492496 {
493497 zipFile ( in , zOut , vPath , resource .getLastModified (), null , entry .getMode (), symlinkTarget );
@@ -584,7 +588,39 @@ protected void zipDir( PlexusIoResource dir, ConcurrentJarCreator zOut, String v
584588 }
585589 }
586590
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 )
588624 {
589625 return new InputStreamSupplier ()
590626 {
0 commit comments