Skip to content

Commit

Permalink
fix resources with zip downgrader
Browse files Browse the repository at this point in the history
  • Loading branch information
wagyourtail committed Mar 27, 2024
1 parent 61d906c commit beb04b1
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 7 deletions.
1 change: 1 addition & 0 deletions downgradetest/src/main/resources/test/testResource2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
another test contents
1 change: 1 addition & 0 deletions downgradetest/src/main/resources/testResource.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
test resource contents
21 changes: 14 additions & 7 deletions src/main/java/xyz/wagyourtail/jvmdg/compile/ZipDowngrader.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, Object>())) {
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<Path>() {
@Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) {
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit beb04b1

Please sign in to comment.