From a8ae4d1a3faa1bd4b5d7b9b34ffae868ed78cb88 Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Tue, 9 Dec 2014 14:16:35 -0800 Subject: [PATCH] Only include zip files when using `spring jar` Update JarCommand to only include nested libraries that are actually zip files. Similar to commit 38585bf3 which introduced the same functionality to the Repackager. Fixes gh-2094 --- .../boot/cli/command/jar/JarCommand.java | 28 ++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/spring-boot-cli/src/main/java/org/springframework/boot/cli/command/jar/JarCommand.java b/spring-boot-cli/src/main/java/org/springframework/boot/cli/command/jar/JarCommand.java index 8931ea06284..51e9356356e 100644 --- a/spring-boot-cli/src/main/java/org/springframework/boot/cli/command/jar/JarCommand.java +++ b/spring-boot-cli/src/main/java/org/springframework/boot/cli/command/jar/JarCommand.java @@ -74,6 +74,8 @@ public class JarCommand extends OptionParsingCommand { private static final Layout LAYOUT = new Layouts.Jar(); + private static final byte[] ZIP_FILE_HEADER = new byte[] { 'P', 'K', 3, 4 }; + public JarCommand() { super("jar", "Create a self-contained " + "executable jar file from a Spring Groovy script", @@ -252,12 +254,36 @@ public class JarCommand extends OptionParsingCommand { private void addDependency(JarWriter writer, File dependency) throws FileNotFoundException, IOException { - if (dependency.isFile()) { + if (dependency.isFile() && isZip(dependency)) { writer.writeNestedLibrary("lib/", new Library(dependency, LibraryScope.COMPILE)); } } + private boolean isZip(File file) { + try { + FileInputStream fileInputStream = new FileInputStream(file); + try { + return isZip(fileInputStream); + } + finally { + fileInputStream.close(); + } + } + catch (IOException ex) { + return false; + } + } + + private boolean isZip(InputStream inputStream) throws IOException { + for (int i = 0; i < ZIP_FILE_HEADER.length; i++) { + if (inputStream.read() != ZIP_FILE_HEADER[i]) { + return false; + } + } + return true; + } + } /**