From 64b8a32bdc0243986f8d841c8560d5e5d4e5b895 Mon Sep 17 00:00:00 2001 From: Ben Fry Date: Sun, 22 Jan 2023 10:37:17 -0500 Subject: [PATCH] clean up utils a little and throw exception to caller in unzip() --- app/src/processing/app/Util.java | 46 +++++++++---------- .../app/contrib/AvailableContribution.java | 7 ++- 2 files changed, 27 insertions(+), 26 deletions(-) diff --git a/app/src/processing/app/Util.java b/app/src/processing/app/Util.java index b03c1a382..f714ca815 100644 --- a/app/src/processing/app/Util.java +++ b/app/src/processing/app/Util.java @@ -464,7 +464,7 @@ public class Util { /** * @param folder source folder to search - * @return a list of .jar and .zip files in that folder + * @return an array of .jar and .zip files in that folder */ static public File[] listJarFiles(File folder) { return folder.listFiles((dir, name) -> @@ -585,9 +585,9 @@ public class Util { if (!entry.isDirectory()) { String name = entry.getName(); - // Avoid META-INF because some jokers but .class files in there + // Avoid META-INF because some jokers put .class files in there // https://github.com/processing/processing/issues/5778 - if (name.endsWith(".class") && !name.startsWith("META-INF/")) { + if (name.endsWith(".class") && !name.contains("META-INF/")) { int slash = name.lastIndexOf('/'); if (slash != -1) { String packageName = name.substring(0, slash); @@ -640,31 +640,27 @@ public class Util { * Extract the contents of a .zip archive into a folder. * Ignores (does not extract) any __MACOSX files from macOS archives. */ - static public void unzip(File zipFile, File dest) { - try { - FileInputStream fis = new FileInputStream(zipFile); - CheckedInputStream checksum = new CheckedInputStream(fis, new Adler32()); - ZipInputStream zis = new ZipInputStream(new BufferedInputStream(checksum)); - ZipEntry entry; - while ((entry = zis.getNextEntry()) != null) { - final String name = entry.getName(); - if (!name.startsWith(("__MACOSX"))) { - File currentFile = new File(dest, name); - if (entry.isDirectory()) { - currentFile.mkdirs(); - } else { - File parentDir = currentFile.getParentFile(); - // Sometimes the directory entries aren't already created - if (!parentDir.exists()) { - parentDir.mkdirs(); - } - currentFile.createNewFile(); - unzipEntry(zis, currentFile); + static public void unzip(File zipFile, File dest) throws IOException { + FileInputStream fis = new FileInputStream(zipFile); + CheckedInputStream checksum = new CheckedInputStream(fis, new Adler32()); + ZipInputStream zis = new ZipInputStream(new BufferedInputStream(checksum)); + ZipEntry entry; + while ((entry = zis.getNextEntry()) != null) { + final String name = entry.getName(); + if (!name.startsWith(("__MACOSX"))) { + File currentFile = new File(dest, name); + if (entry.isDirectory()) { + currentFile.mkdirs(); + } else { + File parentDir = currentFile.getParentFile(); + // Sometimes the directory entries aren't already created + if (!parentDir.exists()) { + parentDir.mkdirs(); } + currentFile.createNewFile(); + unzipEntry(zis, currentFile); } } - } catch (Exception e) { - e.printStackTrace(); } } diff --git a/app/src/processing/app/contrib/AvailableContribution.java b/app/src/processing/app/contrib/AvailableContribution.java index f9d49a879..5e70f50c2 100644 --- a/app/src/processing/app/contrib/AvailableContribution.java +++ b/app/src/processing/app/contrib/AvailableContribution.java @@ -159,7 +159,12 @@ public class AvailableContribution extends Contribution { } return null; } - Util.unzip(contribArchive, tempFolder); + try { + Util.unzip(contribArchive, tempFolder); + } catch (IOException e) { + e.printStackTrace(); + return null; + } LocalContribution installedContrib = null; // Find the first legitimate folder in what we just unzipped