From 5ee7f02b42c62130065b05816b9ba7d3c23b9260 Mon Sep 17 00:00:00 2001 From: Tom Hicks Date: Tue, 22 Sep 2026 21:40:04 -0700 Subject: [PATCH] Catch exceptions and handle graceful zip closes when cancelled --- .../importers/GitRepoImporterService.java | 22 +++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/Android/app/src/main/java/com/majinnaibu/monstercards/importers/GitRepoImporterService.java b/Android/app/src/main/java/com/majinnaibu/monstercards/importers/GitRepoImporterService.java index d3ba4a5..77663c6 100644 --- a/Android/app/src/main/java/com/majinnaibu/monstercards/importers/GitRepoImporterService.java +++ b/Android/app/src/main/java/com/majinnaibu/monstercards/importers/GitRepoImporterService.java @@ -112,12 +112,19 @@ public class GitRepoImporterService { } } - private static int unzipAndFilter(File zipFile, File extractDir, String filterExtension, String subfolder, BooleanSupplier isCancelled) throws IOException { + private static int unzipAndFilter(File zipFile, File extractDir, String filterExtension, String subfolder, BooleanSupplier isCancelled) { int extractedCount = 0; try (ZipInputStream zis = new ZipInputStream(new FileInputStream(zipFile))) { ZipEntry entry; while ((entry = zis.getNextEntry()) != null) { - if (isCancelled.getAsBoolean()) break; + if (isCancelled.getAsBoolean()) { + try { + zis.closeEntry(); + } catch (Exception e) { + Logger.logError("Failed to close zip entry upon cancellation", e); + } + break; + } if (!entry.isDirectory()) { String name = entry.getName(); boolean inSubfolder = subfolder == null || subfolder.isEmpty() || name.contains("/" + subfolder + "/") || name.startsWith(subfolder + "/"); @@ -133,12 +140,19 @@ public class GitRepoImporterService { if (isCancelled.getAsBoolean()) break; fos.write(buffer, 0, count); } + } catch (Exception e) { + Logger.logError("Failed to write extracted file to disk: " + name, e); } - extractedCount++; } } - zis.closeEntry(); + try { + zis.closeEntry(); + } catch (Exception e) { + Logger.logError("Failed to close zip entry", e); + } } + } catch (Exception e) { + Logger.logError("Failed during unzip stream processing", e); } return extractedCount; }