Skip to content

Commit a8ae4d1

Browse files
committed
Only include zip files when using spring jar
Update JarCommand to only include nested libraries that are actually zip files. Similar to commit 38585bf which introduced the same functionality to the Repackager. Fixes gh-2094
1 parent 3523bca commit a8ae4d1

File tree

1 file changed

+27
-1
lines changed
  • spring-boot-cli/src/main/java/org/springframework/boot/cli/command/jar

1 file changed

+27
-1
lines changed

spring-boot-cli/src/main/java/org/springframework/boot/cli/command/jar/JarCommand.java

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ public class JarCommand extends OptionParsingCommand {
7474

7575
private static final Layout LAYOUT = new Layouts.Jar();
7676

77+
private static final byte[] ZIP_FILE_HEADER = new byte[] { 'P', 'K', 3, 4 };
78+
7779
public JarCommand() {
7880
super("jar", "Create a self-contained "
7981
+ "executable jar file from a Spring Groovy script",
@@ -252,12 +254,36 @@ private void addDependencies(JarWriter writer, List<URL> urls)
252254

253255
private void addDependency(JarWriter writer, File dependency)
254256
throws FileNotFoundException, IOException {
255-
if (dependency.isFile()) {
257+
if (dependency.isFile() && isZip(dependency)) {
256258
writer.writeNestedLibrary("lib/", new Library(dependency,
257259
LibraryScope.COMPILE));
258260
}
259261
}
260262

263+
private boolean isZip(File file) {
264+
try {
265+
FileInputStream fileInputStream = new FileInputStream(file);
266+
try {
267+
return isZip(fileInputStream);
268+
}
269+
finally {
270+
fileInputStream.close();
271+
}
272+
}
273+
catch (IOException ex) {
274+
return false;
275+
}
276+
}
277+
278+
private boolean isZip(InputStream inputStream) throws IOException {
279+
for (int i = 0; i < ZIP_FILE_HEADER.length; i++) {
280+
if (inputStream.read() != ZIP_FILE_HEADER[i]) {
281+
return false;
282+
}
283+
}
284+
return true;
285+
}
286+
261287
}
262288

263289
/**

0 commit comments

Comments
 (0)