diff --git a/spring-boot-project/spring-boot-tools/spring-boot-loader-tools/src/main/java/org/springframework/boot/loader/tools/JarWriter.java b/spring-boot-project/spring-boot-tools/spring-boot-loader-tools/src/main/java/org/springframework/boot/loader/tools/JarWriter.java index caedb7fffc9..2316552991d 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-loader-tools/src/main/java/org/springframework/boot/loader/tools/JarWriter.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-loader-tools/src/main/java/org/springframework/boot/loader/tools/JarWriter.java @@ -134,7 +134,7 @@ public class JarWriter implements LoaderClassesWriter, AutoCloseable { Enumeration entries = jarFile.entries(); while (entries.hasMoreElements()) { JarArchiveEntry entry = new JarArchiveEntry(entries.nextElement()); - setUpStoredEntryIfNecessary(jarFile, entry); + setUpEntry(jarFile, entry); try (ZipHeaderPeekInputStream inputStream = new ZipHeaderPeekInputStream( jarFile.getInputStream(entry))) { EntryWriter entryWriter = new InputStreamEntryWriter(inputStream, true); @@ -146,13 +146,15 @@ public class JarWriter implements LoaderClassesWriter, AutoCloseable { } } - private void setUpStoredEntryIfNecessary(JarFile jarFile, JarArchiveEntry entry) - throws IOException { + private void setUpEntry(JarFile jarFile, JarArchiveEntry entry) throws IOException { try (ZipHeaderPeekInputStream inputStream = new ZipHeaderPeekInputStream( jarFile.getInputStream(entry))) { if (inputStream.hasZipHeader() && entry.getMethod() != ZipEntry.STORED) { new CrcAndSize(inputStream).setupStoredEntry(entry); } + else { + entry.setCompressedSize(-1); + } } } diff --git a/spring-boot-project/spring-boot-tools/spring-boot-loader-tools/src/test/java/org/springframework/boot/loader/tools/RepackagerTests.java b/spring-boot-project/spring-boot-tools/spring-boot-loader-tools/src/test/java/org/springframework/boot/loader/tools/RepackagerTests.java index b8416d00880..be35e8bb9b3 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-loader-tools/src/test/java/org/springframework/boot/loader/tools/RepackagerTests.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-loader-tools/src/test/java/org/springframework/boot/loader/tools/RepackagerTests.java @@ -18,6 +18,7 @@ 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; @@ -25,11 +26,14 @@ import java.util.ArrayList; import java.util.Calendar; import java.util.Enumeration; import java.util.List; +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.apache.commons.compress.archivers.zip.ZipArchiveEntry; import org.apache.commons.compress.archivers.zip.ZipFile; @@ -638,6 +642,28 @@ public class RepackagerTests { "META-INF/MANIFEST.MF", "a/", "a/b/", "a/b/C.class"); } + @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 File createLibrary() throws IOException { TestJarFile library = new TestJarFile(this.temporaryFolder); library.addClass("com/example/library/Library.class",