Skip to content

Commit a50646b

Browse files
committed
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
1 parent 9a49e8e commit a50646b

File tree

2 files changed

+30
-1
lines changed
  • spring-boot-tools/spring-boot-loader-tools/src

2 files changed

+30
-1
lines changed

spring-boot-tools/spring-boot-loader-tools/src/main/java/org/springframework/boot/loader/tools/JarWriter.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,9 @@ void writeEntries(JarFile jarFile, EntryTransformer entryTransformer)
140140
inputStream = new ZipHeaderPeekInputStream(
141141
jarFile.getInputStream(entry));
142142
}
143+
else {
144+
entry.setCompressedSize(-1);
145+
}
143146
EntryWriter entryWriter = new InputStreamEntryWriter(inputStream, true);
144147
JarEntry transformedEntry = entryTransformer.transform(entry);
145148
if (transformedEntry != null) {

spring-boot-tools/spring-boot-loader-tools/src/test/java/org/springframework/boot/loader/tools/RepackagerTests.java

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2017 the original author or authors.
2+
* Copyright 2012-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,15 +18,19 @@
1818

1919
import java.io.ByteArrayInputStream;
2020
import java.io.File;
21+
import java.io.FileOutputStream;
2122
import java.io.IOException;
2223
import java.nio.file.Files;
2324
import java.nio.file.attribute.PosixFilePermission;
2425
import java.util.Calendar;
26+
import java.util.Random;
2527
import java.util.jar.Attributes;
2628
import java.util.jar.JarEntry;
2729
import java.util.jar.JarFile;
2830
import java.util.jar.Manifest;
31+
import java.util.zip.Deflater;
2932
import java.util.zip.ZipEntry;
33+
import java.util.zip.ZipOutputStream;
3034

3135
import org.junit.Before;
3236
import org.junit.Rule;
@@ -613,6 +617,28 @@ public void metaInfAopXmlIsMovedBeneathBootInfClassesWhenRepackaged()
613617
}
614618
}
615619

620+
@Test
621+
public void jarThatUsesCustomCompressionConfigurationCanBeRepackaged()
622+
throws IOException {
623+
File source = this.temporaryFolder.newFile("source.jar");
624+
ZipOutputStream output = new ZipOutputStream(new FileOutputStream(source)) {
625+
{
626+
this.def = new Deflater(Deflater.NO_COMPRESSION, true);
627+
}
628+
};
629+
byte[] data = new byte[1024 * 1024];
630+
new Random().nextBytes(data);
631+
ZipEntry entry = new ZipEntry("entry.dat");
632+
output.putNextEntry(entry);
633+
output.write(data);
634+
output.closeEntry();
635+
output.close();
636+
File dest = this.temporaryFolder.newFile("dest.jar");
637+
Repackager repackager = new Repackager(source);
638+
repackager.setMainClass("com.example.Main");
639+
repackager.repackage(dest, NO_LIBRARIES);
640+
}
641+
616642
private boolean hasLauncherClasses(File file) throws IOException {
617643
return hasEntry(file, "org/springframework/boot/")
618644
&& hasEntry(file, "org/springframework/boot/loader/JarLauncher.class");

0 commit comments

Comments
 (0)