From a50646b7cc3ad941e748dfb450077e3a73706205 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Mon, 9 Jul 2018 13:16:41 +0100 Subject: [PATCH] Fix repackaging of jars with non-default compression configuration Previously, if a jar that used custom compression configuration was repackaged, a failure may occur if an entry in the repackaged jar had a different compressed size to the entry in the source jar. This commit updates JarWriter to clear the input entry's compressed size (by setting it to -1) so that the repackaged entry's compressed size does not have to match that of the input entry. Closes gh-13720 --- .../boot/loader/tools/JarWriter.java | 3 ++ .../boot/loader/tools/RepackagerTests.java | 28 ++++++++++++++++++- 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/spring-boot-tools/spring-boot-loader-tools/src/main/java/org/springframework/boot/loader/tools/JarWriter.java b/spring-boot-tools/spring-boot-loader-tools/src/main/java/org/springframework/boot/loader/tools/JarWriter.java index 9d6608c33c7..d61926ed9b7 100644 --- a/spring-boot-tools/spring-boot-loader-tools/src/main/java/org/springframework/boot/loader/tools/JarWriter.java +++ b/spring-boot-tools/spring-boot-loader-tools/src/main/java/org/springframework/boot/loader/tools/JarWriter.java @@ -140,6 +140,9 @@ public class JarWriter implements LoaderClassesWriter { inputStream = new ZipHeaderPeekInputStream( jarFile.getInputStream(entry)); } + else { + entry.setCompressedSize(-1); + } EntryWriter entryWriter = new InputStreamEntryWriter(inputStream, true); JarEntry transformedEntry = entryTransformer.transform(entry); if (transformedEntry != null) { diff --git a/spring-boot-tools/spring-boot-loader-tools/src/test/java/org/springframework/boot/loader/tools/RepackagerTests.java b/spring-boot-tools/spring-boot-loader-tools/src/test/java/org/springframework/boot/loader/tools/RepackagerTests.java index 91a2b88ced3..f00dee16dd7 100644 --- a/spring-boot-tools/spring-boot-loader-tools/src/test/java/org/springframework/boot/loader/tools/RepackagerTests.java +++ b/spring-boot-tools/spring-boot-loader-tools/src/test/java/org/springframework/boot/loader/tools/RepackagerTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2017 the original author or authors. + * Copyright 2012-2018 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -18,15 +18,19 @@ package org.springframework.boot.loader.tools; import java.io.ByteArrayInputStream; import java.io.File; +import java.io.FileOutputStream; import java.io.IOException; import java.nio.file.Files; import java.nio.file.attribute.PosixFilePermission; import java.util.Calendar; +import java.util.Random; import java.util.jar.Attributes; import java.util.jar.JarEntry; import java.util.jar.JarFile; import java.util.jar.Manifest; +import java.util.zip.Deflater; import java.util.zip.ZipEntry; +import java.util.zip.ZipOutputStream; import org.junit.Before; import org.junit.Rule; @@ -613,6 +617,28 @@ public class RepackagerTests { } } + @Test + public void jarThatUsesCustomCompressionConfigurationCanBeRepackaged() + throws IOException { + File source = this.temporaryFolder.newFile("source.jar"); + ZipOutputStream output = new ZipOutputStream(new FileOutputStream(source)) { + { + this.def = new Deflater(Deflater.NO_COMPRESSION, true); + } + }; + byte[] data = new byte[1024 * 1024]; + new Random().nextBytes(data); + ZipEntry entry = new ZipEntry("entry.dat"); + output.putNextEntry(entry); + output.write(data); + output.closeEntry(); + output.close(); + File dest = this.temporaryFolder.newFile("dest.jar"); + Repackager repackager = new Repackager(source); + repackager.setMainClass("com.example.Main"); + repackager.repackage(dest, NO_LIBRARIES); + } + private boolean hasLauncherClasses(File file) throws IOException { return hasEntry(file, "org/springframework/boot/") && hasEntry(file, "org/springframework/boot/loader/JarLauncher.class");