繁体   English   中英

以编程方式替换 JAR 中的 MANIFEST.MF 文件

[英]Replacing the MANIFEST.MF file in a JAR programmatically

我想在创建JAR排除某些Class-Path条目后修改MANIFEST.MF 为此,我决定使用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 似乎起始字符被切断了。 这可能是什么原因,或者是否有另一种有意义的可能性来替换JAR s(实际上只是ZIP s)内的文件?

maven依赖:

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

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

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

你可以使用这样的东西:

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 (请参阅技术说明)。

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM