- Improve error-handling in file-compression.

- Update dependencies.
This commit is contained in:
Lampros Smyrnaios 2023-10-04 16:08:38 +03:00
parent 2895668417
commit 18cc9e0e68
2 changed files with 12 additions and 7 deletions

View File

@ -1,5 +1,5 @@
plugins {
id 'org.springframework.boot' version '2.7.15'
id 'org.springframework.boot' version '2.7.16'
id 'io.spring.dependency-management' version '1.1.3'
id 'java'
}
@ -44,11 +44,11 @@ dependencies {
// https://mvnrepository.com/artifact/com.google.code.gson/gson
implementation 'com.google.code.gson:gson:2.10.1'
implementation("org.apache.commons:commons-compress:1.23.0") {
implementation("org.apache.commons:commons-compress:1.24.0") {
exclude group: 'com.github.luben', module: 'zstd-jni'
}
implementation 'com.github.luben:zstd-jni:1.5.5-5' // Even though this is part of the above dependency, the Apache commons rarely updates it, while the zstd team makes improvements very often.
// Also, for compressing, we strangely need it, otherwise it does not work.
implementation 'com.github.luben:zstd-jni:1.5.5-6' // Even though this is part of the above dependency, the Apache commons rarely updates it, while the zstd team makes improvements very often.
// Also, for compressing, we strangely need it to be explicitly declared independently, otherwise it does not work.
testImplementation 'org.springframework.security:spring-security-test'
testImplementation "org.springframework.boot:spring-boot-starter-test"

View File

@ -12,6 +12,7 @@ import java.io.BufferedOutputStream;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.NoSuchFileException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
@ -85,7 +86,9 @@ public class FilesCompressor {
}
}
if ( numTarredFiles != filesToTar.size() )
if ( numTarredFiles == 0 ) {
throw new RuntimeException("None of the requested (" + filesToTar.size() + ") could be tarred, for assignments_" + assignmentsCounter + ", batch_" + tarBatchCounter);
} else if ( numTarredFiles != filesToTar.size() )
logger.warn("The number of \"numTarredFiles\" (" + numTarredFiles + ") is different from the number of files requested to be tarred (" + filesToTar.size() + "), for assignments_" + assignmentsCounter + ", batch_" + tarBatchCounter);
// Still, some files may have been tarred, so we move on. It's up to the Controller, to handle such case.
@ -98,8 +101,7 @@ public class FilesCompressor {
boolean shouldCloseEntry = false; // Useful in order to know if we should close the entry (an Exception may appear when initializing the stream, and so we should not try to close it).
Path fullFileNamePath = Paths.get(baseDir + fileName);
try ( BufferedInputStream fis = new BufferedInputStream(Files.newInputStream(fullFileNamePath), bufferSize) )
{
try ( BufferedInputStream fis = new BufferedInputStream(Files.newInputStream(fullFileNamePath), bufferSize) ) {
TarArchiveEntry entry = new TarArchiveEntry(fileName);
entry.setSize(Files.size(fullFileNamePath)); // Yes, tar requires that we set the size beforehand..
taos.putArchiveEntry(entry);
@ -109,6 +111,9 @@ public class FilesCompressor {
while ( (readByte = fis.read()) != -1 ) {
taos.write(readByte);
}
} catch (NoSuchFileException nsfe) {
logger.error("NoSuchFileException: " + nsfe.getMessage());
return false;
} catch (Exception e) {
logger.error("", e);
return false;