com.alipay.sofa.koupleless.base.build.plugin.common.JarFileUtils Maven / Gradle / Ivy
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alipay.sofa.koupleless.base.build.plugin.common;
import com.google.common.base.Preconditions;
import lombok.SneakyThrows;
import java.io.*;
import java.util.HashMap;
import java.util.Map;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.jar.JarInputStream;
import java.util.jar.Manifest;
import java.util.regex.Pattern;
/**
* jar file utils
*
* @author CodeNoobKing
* @version 1.0.0
*/
public class JarFileUtils {
/**
* get content as file lines inside a bundled jar file.
*
* @param file jarfile
* @param entryPattern the target file.
* @return the content as list of string.
*/
@SneakyThrows
public static Map getFileContentAsLines(File file, Pattern entryPattern) {
Map result = new HashMap<>();
try (JarInputStream jin = new JarInputStream(new FileInputStream(file))) {
JarEntry entry = null;
while ((entry = jin.getNextJarEntry()) != null) {
if (!entryPattern.matcher(entry.getName()).matches() || entry.isDirectory()) {
continue;
}
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
int nRead = -1;
byte[] data = new byte[1024];
while ((nRead = jin.read(data, 0, data.length)) != -1) {
buffer.write(data, 0, nRead);
}
byte[] byteArray = buffer.toByteArray();
Byte[] boxing = new Byte[byteArray.length];
for (int i = 0; i < byteArray.length; i++) {
boxing[i] = byteArray[i];
}
result.put(entry.getName(), boxing);
}
}
return result;
}
@SneakyThrows
public static Map getMainAttributes(String bizUrl) {
try (JarFile jarFile = new JarFile(bizUrl)) {
Manifest manifest = jarFile.getManifest();
Preconditions.checkState(manifest != null, "Manifest file not found in the JAR.");
Map result = new HashMap<>();
manifest.getMainAttributes().forEach((k, v) -> result.put(k.toString(), v));
return result;
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy