From beb04b198320b7815ce65efd5bf700df6ea38d95 Mon Sep 17 00:00:00 2001 From: Wagyourtail Date: Wed, 27 Mar 2024 00:10:12 -0500 Subject: [PATCH] fix resources with zip downgrader --- .../src/main/resources/test/testResource2.txt | 1 + .../src/main/resources/testResource.txt | 1 + .../jvmdg/compile/ZipDowngrader.java | 21 ++++++++++++------- 3 files changed, 16 insertions(+), 7 deletions(-) create mode 100644 downgradetest/src/main/resources/test/testResource2.txt create mode 100644 downgradetest/src/main/resources/testResource.txt diff --git a/downgradetest/src/main/resources/test/testResource2.txt b/downgradetest/src/main/resources/test/testResource2.txt new file mode 100644 index 00000000..d67bb671 --- /dev/null +++ b/downgradetest/src/main/resources/test/testResource2.txt @@ -0,0 +1 @@ +another test contents \ No newline at end of file diff --git a/downgradetest/src/main/resources/testResource.txt b/downgradetest/src/main/resources/testResource.txt new file mode 100644 index 00000000..58bd22a1 --- /dev/null +++ b/downgradetest/src/main/resources/testResource.txt @@ -0,0 +1 @@ +test resource contents \ No newline at end of file diff --git a/src/main/java/xyz/wagyourtail/jvmdg/compile/ZipDowngrader.java b/src/main/java/xyz/wagyourtail/jvmdg/compile/ZipDowngrader.java index ad2a5113..1947a3af 100644 --- a/src/main/java/xyz/wagyourtail/jvmdg/compile/ZipDowngrader.java +++ b/src/main/java/xyz/wagyourtail/jvmdg/compile/ZipDowngrader.java @@ -50,6 +50,18 @@ public static void downgradeZip(final ClassDowngrader downgrader, Path zip, Set< final URLClassLoader extraClasspath = new URLClassLoader(classpath.toArray(new URL[0]), ZipDowngrader.class.getClassLoader()); try (final FileSystem zipfs = Utils.openZipFileSystem(zip, new HashMap())) { try (ZipOutputStream zos = new ZipOutputStream(Files.newOutputStream(output))) { + // write manifest seperately, since it's supposed to be first + Path manifest = zipfs.getPath("META-INF/MANIFEST.MF"); + if (Files.exists(manifest)) { + byte[] bytes = Utils.readAllBytes(Files.newInputStream(manifest)); + ZipEntry newEntry = new ZipEntry("META-INF/MANIFEST.MF"); + newEntry.setTime(Files.getLastModifiedTime(manifest).toMillis()); + zos.putNextEntry(newEntry); + zos.write(bytes); + zos.flush(); + zos.closeEntry(); + } + Files.walkFileTree(zipfs.getPath("/"), new FileVisitor() { @Override public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) { @@ -115,13 +127,8 @@ public byte[] apply(String s) { } catch (IllegalClassFormatException e) { throw new IOException("Failed to downgrade class", e); } - } else { - String fileName = file.getFileName().toString(); - // remove leading / - if (fileName.startsWith("/")) { - fileName = fileName.substring(1); - } - ZipEntry newEntry = new ZipEntry(fileName); + } else if (!file.toString().equals("/META-INF/MANIFEST.MF")) { + ZipEntry newEntry = new ZipEntry(file.toString().substring(1)); newEntry.setTime(attrs.lastModifiedTime().toMillis()); zos.putNextEntry(newEntry); zos.write(bytes);