- Optimize zip-file creation.

- Update dependencies.
This commit is contained in:
Lampros Smyrnaios 2022-05-26 15:24:36 +03:00
parent a1f750a0aa
commit d6e94912a4
3 changed files with 10 additions and 14 deletions

View File

@ -1,5 +1,5 @@
plugins {
id 'org.springframework.boot' version '2.6.6'
id 'org.springframework.boot' version '2.7.0'
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
id 'java'
}
@ -26,7 +26,7 @@ dependencies {
implementation("org.springframework.security:spring-security-config")
//implementation("io.jsonwebtoken:jjwt:0.9.1") // Use this in case we use auth-tokens later on.
implementation "org.projectlombok:lombok:1.18.22"
implementation 'org.projectlombok:lombok:1.18.24'
// Enable the validation annotations.
//implementation group: 'javax.validation', name: 'validation-api', version: '2.0.1.Final'

View File

@ -5,6 +5,7 @@ import org.slf4j.LoggerFactory;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
@ -22,7 +23,7 @@ public class FilesZipper
int numZippedFiles = 0;
File zipFile = new File(zipFilename);
try ( ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zipFile), StandardCharsets.UTF_8) )
try ( ZipOutputStream zos = new ZipOutputStream(Files.newOutputStream(zipFile.toPath()), StandardCharsets.UTF_8) )
{
for ( String file : filesToZip ) {
if ( zipAFile(file, zos, baseDirectory) )
@ -37,20 +38,16 @@ public class FilesZipper
}
private static final int BUFFER_SIZE = 3145728; // 3MB (average fullText-size)
private static final byte[] dataBuffer = new byte[BUFFER_SIZE];
// This method is "synchronized" to avoid any future problems with shared-buffer, if the requests are asynchronous.
private static synchronized boolean zipAFile(String fileName, ZipOutputStream zos, String baseDir)
private static boolean zipAFile(String fileName, ZipOutputStream zos, String baseDir)
{
boolean shouldCloseEntry = false; // Useful in order to close the entry in case of an exception.
boolean shouldCloseEntry = false; // Useful in order to know if we should close the entry (an Exception may appear, and so we should not try to close it).
String fullFileName = baseDir + fileName;
try ( BufferedInputStream bis = new BufferedInputStream(new FileInputStream(fullFileName), BUFFER_SIZE) ) {
try (FileInputStream fis = new FileInputStream(fullFileName)) {
zos.putNextEntry(new ZipEntry(fileName));
shouldCloseEntry = true;
int count;
while ( (count = bis.read(dataBuffer, 0, BUFFER_SIZE)) != -1 ) {
zos.write(dataBuffer, 0, count);
int readByte;
while ( (readByte = fis.read()) != -1 ) {
zos.write(readByte);
}
} catch (FileNotFoundException fnfe) {
logger.error("Error zipping file: " + fullFileName, fnfe.getMessage());

View File

@ -26,7 +26,6 @@ public class UriBuilder {
sslEnabled = "false";
}
baseUrl += sslEnabled.equals("true") ? "s" : "";
baseUrl += "://";
String hostName = getPublicIP();