#StackBounty: #java #jar #zip #manifest.mf #zip4j Replacing the MANIFEST.MF file in a JAR programmatically

Bounty: 50

I want to modify the MANIFEST.MF after creating the JAR to exclude certain Class-Path entries. For this I decided to use zip4j. Extraction seems to work fine but for putting the MANIFEST.MF file back into the JAR, I use the following code:

String metaInfFolderName = "META-INF";
Path extractedManifestFilePath = Paths.get("...");
ZipFile zipFile = new ZipFile("Test-Zip-File.zip");
ZipParameters zipParameters = new ZipParameters();
zipParameters.setDefaultFolderPath(metaInfFolderName);
zipFile.addFile(extractedManifestFilePath.toFile(), zipParameters);

However, this code does not work as expected: The parent directory always ends up being named NF instead of the full META-INF. It seems like the starting characters are cut off. What could be the reason for this or is there another meaningful possibility to replace files inside JARs (which are essentially just ZIPs)?

maven dependency:

<dependency>
    <groupId>net.lingala.zip4j</groupId>
    <artifactId>zip4j</artifactId>
    <version>2.6.1</version>
</dependency>

Furthermore I tried using the jar utility like described here but when invoking the command jar uf MyJAR.jar META-INF/MANIFEST.MF the MANIFEST.MF inside the JAR gets deleted instead of replaced. Using the zip utility via zip -ur MyJAR.jar "META-INF/MANIFEST.MF" works but corrupts the JAR file so it is no longer runnable:

Error: An unexpected error occurred while trying to open file MyJAR.jar


Get this bounty!!!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.