fix(cbx): drain NightCompress InputStream before closing (#1004)

This commit is contained in:
James Ward
2026-05-01 00:52:58 -04:00
committed by GitHub
parent f44661318b
commit 12e721fa0f
@@ -14,6 +14,7 @@ import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.locks.ReentrantLock;
@@ -97,7 +98,13 @@ public class ArchiveService {
lock.lock();
try (InputStream inputStream = Archive.getInputStream(path, entryName)) {
if (inputStream != null) {
return inputStream.transferTo(outputStream);
try {
return inputStream.transferTo(outputStream);
} finally {
// NightCompress fails with a SIGSEGV if you do not read the
// entirety of the input stream from the zip.
inputStream.transferTo(OutputStream.nullOutputStream());
}
}
} catch (Exception e) {
throw new IOException("Failed to extract from archive: " + e.getMessage(), e);
@@ -185,16 +192,24 @@ public class ArchiveService {
requireAvailable();
ReentrantLock lock = getFileLock(path);
lock.lock();
try (InputStream inputStream = Archive.getInputStream(path, entryName)) {
if (inputStream != null) {
return Files.copy(inputStream, outputPath);
}
boolean hasCreatedFile = false;
try (OutputStream outputStream = Files.newOutputStream(outputPath, StandardOpenOption.CREATE_NEW, StandardOpenOption.WRITE)) {
hasCreatedFile = true;
return transferEntryTo(path, entryName, outputStream);
} catch (Exception e) {
if (hasCreatedFile) {
try {
Files.deleteIfExists(outputPath);
} catch (Exception ce) {
e.addSuppressed(ce);
}
}
throw new IOException("Failed to extract from archive: " + e.getMessage(), e);
} finally {
lock.unlock();
}
throw new IOException("Entry not found in archive");
}
}