3

我想MANIFEST.MF在创建后修改JAR排除某些Class-Path条目。为此,我决定使用zip4j. 提取似乎工作正常,但要将MANIFEST.MF文件放回JAR,我使用以下代码:

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);

但是,此代码无法按预期工作:父目录总是最终被命名NF而不是完整的META-INF. 似乎起始字符被切断了。这可能是什么原因,或者是否有另一种有意义的可能性来替换JARs 内的文件(本质上只是ZIPs)?

maven依赖:

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

此外,我尝试使用此处jar描述的实用程序,但是在调用命令时,内部的命令被删除而不是被替换。通过工作使用该实用程序但会损坏文件,因此它不再可运行:jar uf MyJAR.jar META-INF/MANIFEST.MFMANIFEST.MFJARzipzip -ur MyJAR.jar "META-INF/MANIFEST.MF"JAR

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

2 回答 2

2

你可以使用这样的东西:

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URI;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;
import java.util.jar.Attributes;
import java.util.jar.Attributes.Name;
import java.util.jar.Manifest;

public class ManifestManipulator {

    public static final void main(String... args) throws IOException {
        if (args.length == 0) {
            throw new IllegalArgumentException("at least the path to the JAR file expected!");
        }
        Path jarPath = Paths.get(args[0]);
        Set<String> attributeNames = args.length > 1 ? new LinkedHashSet<>(List.of(args).subList(1, args.length)) : Set.of();

        if (!attributeNames.isEmpty()) {
            ManifestManipulator manifestManipulator = new ManifestManipulator();
            manifestManipulator.removeManifestEntries(jarPath, attributeNames);
        } else {
            System.out.println("Warning: no entries specified to remove!");
        }
    }

    private void removeManifestEntries(Path jarPath, Set<String> attributeNames) throws IOException {
        System.out.println("Going to remove: " + attributeNames);
        try (FileSystem jarFS = FileSystems.newFileSystem(URI.create("jar:" + jarPath.toUri()), Map.of())) {
            Path manifestPath = jarFS.getPath("META-INF", "MANIFEST.MF");
            Manifest manifest = readManifest(manifestPath);
            Attributes mainAttributes = manifest.getMainAttributes();
            System.out.println("Found main attribute names: " + mainAttributes.keySet());

            boolean removed = mainAttributes.entrySet().removeIf(entry -> attributeNames.contains(((Name) entry.getKey()).toString()));
            if (removed) {
                writeManifest(manifestPath, manifest);
            } else {
                System.out.println("Warning: nothing removed");
            }
        }
    }

    private Manifest readManifest(Path manifestPath) throws IOException {
        try (InputStream is = Files.newInputStream(manifestPath)) {
            return new Manifest(is);
        }
    }

    private void writeManifest(Path manifestPath, Manifest manifest) throws IOException {
        try (OutputStream os = Files.newOutputStream(manifestPath)) {
            manifest.write(os);
        }
    }

}

请确保您已添加jdk.zipfs模块,该模块将为ZIP/JAR 文件提供FileSystemProvider (请参阅技术说明)。

于 2020-06-18T20:10:08.243 回答
1

使用 Java 8 更改 ZIP 或 JAR 文件的内容不需要任何外部依赖项。

public static void main(String[] args) throws IOException {
    Path zipFilePath = Paths.get("path/to/my/app.jar");
    try (FileSystem zipFileSystem = FileSystems.newFileSystem(zipFilePath, null)) {
        Path manifestFile = zipFileSystem.getPath("META-INF/MANIFEST.MF");
        String newManifestContent;
        // Read from MANIFEST.MF.
        try (Stream<String> lines = Files.lines(manifestFile, StandardCharsets.UTF_8)) {
            newManifestContent = lines.filter(l -> !l.startsWith("Class-Path entry I want to remove"))
                    .collect(Collectors.joining("\n"));
        }
        // Replace MANIFEST.MF content.
        Files.write(manifestFile, newManifestContent.getBytes(StandardCharsets.UTF_8), StandardOpenOption.TRUNCATE_EXISTING);
    }
}
于 2020-06-25T08:57:00.803 回答