Catch exceptions and handle graceful zip closes when cancelled
This commit is contained in:
@@ -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;
|
int extractedCount = 0;
|
||||||
try (ZipInputStream zis = new ZipInputStream(new FileInputStream(zipFile))) {
|
try (ZipInputStream zis = new ZipInputStream(new FileInputStream(zipFile))) {
|
||||||
ZipEntry entry;
|
ZipEntry entry;
|
||||||
while ((entry = zis.getNextEntry()) != null) {
|
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()) {
|
if (!entry.isDirectory()) {
|
||||||
String name = entry.getName();
|
String name = entry.getName();
|
||||||
boolean inSubfolder = subfolder == null || subfolder.isEmpty() || name.contains("/" + subfolder + "/") || name.startsWith(subfolder + "/");
|
boolean inSubfolder = subfolder == null || subfolder.isEmpty() || name.contains("/" + subfolder + "/") || name.startsWith(subfolder + "/");
|
||||||
@@ -133,13 +140,20 @@ public class GitRepoImporterService {
|
|||||||
if (isCancelled.getAsBoolean()) break;
|
if (isCancelled.getAsBoolean()) break;
|
||||||
fos.write(buffer, 0, count);
|
fos.write(buffer, 0, count);
|
||||||
}
|
}
|
||||||
}
|
} catch (Exception e) {
|
||||||
extractedCount++;
|
Logger.logError("Failed to write extracted file to disk: " + name, e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
try {
|
||||||
zis.closeEntry();
|
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;
|
return extractedCount;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user