Skip to content

Respect order of META-INF/ and META-INF/MANIFEST.MF entries in a JAR file #189

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,12 @@ public class ConcurrentJarCreator

private final boolean compressAddedZips;

private final ScatterZipOutputStream directories;

private final ScatterZipOutputStream metaInfDir;

private final ScatterZipOutputStream manifest;

private final ScatterZipOutputStream directories;

private final ScatterZipOutputStream synchronousEntries;

private final ParallelScatterZipCreator parallelScatterZipCreator;
Expand Down Expand Up @@ -123,9 +123,9 @@ public ConcurrentJarCreator( boolean compressAddedZips, int nThreads ) throws IO
{
this.compressAddedZips = compressAddedZips;
ScatterGatherBackingStoreSupplier defaultSupplier = new DeferredSupplier( 100000000 / nThreads );
directories = createDeferred( defaultSupplier );
manifest = createDeferred( defaultSupplier );
metaInfDir = createDeferred( defaultSupplier );
manifest = createDeferred( defaultSupplier );
directories = createDeferred( defaultSupplier );
synchronousEntries = createDeferred( defaultSupplier );
parallelScatterZipCreator = new ParallelScatterZipCreator( Executors.newFixedThreadPool( nThreads ),
defaultSupplier );
Expand All @@ -152,20 +152,10 @@ public void addArchiveEntry( final ZipArchiveEntry zipArchiveEntry, final InputS
{
throw new IllegalArgumentException( "Method must be set on the supplied zipArchiveEntry" );
}
if ( zipArchiveEntry.isDirectory() && !zipArchiveEntry.isUnixSymlink() )
{
final ByteArrayInputStream payload = new ByteArrayInputStream( new byte[]
{
} );

directories.addArchiveEntry( createZipArchiveEntryRequest( zipArchiveEntry, createInputStreamSupplier(
payload ) ) );

payload.close();
}
else if ( "META-INF".equals( zipArchiveEntry.getName() ) || "META-INF/".equals( zipArchiveEntry.getName() ) )
if ( "META-INF".equals( zipArchiveEntry.getName() ) || "META-INF/".equals( zipArchiveEntry.getName() ) )
{
InputStream payload = source.get();
// TODO This should be enforced because META-INF non-directory does not make any sense?!
if ( zipArchiveEntry.isDirectory() )
{
zipArchiveEntry.setMethod( ZipEntry.STORED );
Expand All @@ -178,6 +168,7 @@ else if ( "META-INF".equals( zipArchiveEntry.getName() ) || "META-INF/".equals(
else if ( "META-INF/MANIFEST.MF".equals( zipArchiveEntry.getName() ) )
{
InputStream payload = source.get();
// TODO This should be enforced because META-INF/MANIFEST as non-file does not make any sense?!
if ( zipArchiveEntry.isDirectory() )
{
zipArchiveEntry.setMethod( ZipEntry.STORED );
Expand All @@ -187,6 +178,17 @@ else if ( "META-INF/MANIFEST.MF".equals( zipArchiveEntry.getName() ) )

payload.close();
}
else if ( zipArchiveEntry.isDirectory() && !zipArchiveEntry.isUnixSymlink() )
{
final ByteArrayInputStream payload = new ByteArrayInputStream( new byte[]
{
} );

directories.addArchiveEntry( createZipArchiveEntryRequest( zipArchiveEntry, createInputStreamSupplier(
payload ) ) );

payload.close();
}
else if ( addInParallel )
{
parallelScatterZipCreator.addArchiveEntry( createEntrySupplier( zipArchiveEntry, source ) );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

Expand All @@ -55,8 +57,11 @@ public void testCreateJar()
// verify that the JAR file is created and contains the expected files
try ( ZipFile resultingArchive = new ZipFile( jarFile ) )
{
// verify that the JAR file contains manifest file
assertNotNull( resultingArchive.getEntry( "META-INF/MANIFEST.MF" ) );
// verify that the JAR file contains manifest directory and file
// and that those are the first two entries.
Enumeration<? extends ZipEntry> resultingEntries = resultingArchive.entries();
assertEquals( "META-INF/", resultingEntries.nextElement().getName() );
assertEquals( "META-INF/MANIFEST.MF", resultingEntries.nextElement().getName() );

// verify the JAR contains the class and it is not corrupted
ZipEntry classFileEntry = resultingArchive.getEntry( "com/example/app/Main.class" );
Expand Down