<executable>true</executable>官方說明
https://docs.spring.io/spring-boot/docs/current/reference/html/deployment.html#deployment.installing
package com.denghb.util;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
/**
* spring-boot-maven-plugin <executable>true</executable>
* 打包后導致不能正常解壓jar包前面的信息去除
* @author denghb
* @since 2021/10/26
*/
public class SpringBootJarCanUnpack {
public static void main(String[] args) {
String source = "D:\\spring-boot-executable-package-1.0-SNAPSHOT.jar";
String target = "D:\\spring-boot-package-1.0-SNAPSHOT.jar";
try (InputStream in = new FileInputStream(source);
OutputStream out = new FileOutputStream(target)) {
// <executable>false</executable> 正常jar的開始字節
byte[] startBytes = new byte[]{80, 75, 3, 4, 20, 0, 8, 8, 8, 0};
flag:
while (true) {
int i = 0;
byte[] bytes = new byte[1];
while (-1 != in.read(bytes)) {
if (bytes[0] == startBytes[i]) {
if (i == startBytes.length - 1) {
break flag;
}
}
if (i == startBytes.length - 1) {
break;
}
i++;
}
}
out.write(startBytes);
int len = 0;
byte[] bytes = new byte[64];
while (-1 != (len = in.read(bytes))) {
out.write(bytes, 0, len);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}