aboutsummaryrefslogtreecommitdiffstats
path: root/bundle-plugin/src/main/java/com/yahoo/container/plugin/util/JarFiles.java
blob: 72d2adae13ee939973bab042c7cc0fcf6cbc3e88 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.container.plugin.util;

import java.io.File;
import java.io.InputStream;
import java.util.Optional;
import java.util.jar.JarFile;
import java.util.jar.Manifest;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

/**
 * @author Tony Vaagenes
 * @author ollivir
 */
public class JarFiles {
    public static <T> T withJarFile(File file, ThrowingFunction<JarFile, T> action) {
        try (JarFile jar = new JarFile(file)) {
            return action.apply(jar);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static <T> T withInputStream(ZipFile zipFile, ZipEntry zipEntry, ThrowingFunction<InputStream, T> action) {
        try (InputStream is = zipFile.getInputStream(zipEntry)) {
            return action.apply(is);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static Optional<Manifest> getManifest(File jarFile) {
        return withJarFile(jarFile, jar -> Optional.ofNullable(jar.getManifest()));
    }
}