From 2e7677042ee51690595d849616269329b2c99740 Mon Sep 17 00:00:00 2001 From: dmitry-weirdo Date: Fri, 22 Nov 2024 02:19:29 +0100 Subject: [PATCH] feat: #81 Migrate ZipUtils to Kotlin. Close the ZipFile resource correctly. --- .../klavogonki/statistics/zip/ZipUtils.java | 24 ------------------- .../ru/klavogonki/statistics/zip/ZipUtils.kt | 23 ++++++++++++++++++ 2 files changed, 23 insertions(+), 24 deletions(-) delete mode 100644 kgstatsSrv/src/main/java/ru/klavogonki/statistics/zip/ZipUtils.java create mode 100644 kgstatsSrv/src/main/kotlin/ru/klavogonki/statistics/zip/ZipUtils.kt diff --git a/kgstatsSrv/src/main/java/ru/klavogonki/statistics/zip/ZipUtils.java b/kgstatsSrv/src/main/java/ru/klavogonki/statistics/zip/ZipUtils.java deleted file mode 100644 index 4e00d10e2..000000000 --- a/kgstatsSrv/src/main/java/ru/klavogonki/statistics/zip/ZipUtils.java +++ /dev/null @@ -1,24 +0,0 @@ -package ru.klavogonki.statistics.zip; - -import lombok.extern.log4j.Log4j2; -import net.lingala.zip4j.ZipFile; -import net.lingala.zip4j.exception.ZipException; - -@Log4j2 -public final class ZipUtils { - - private ZipUtils() { - } - - public static void zipFile(String filePath, String zipFilePath) { - try { - new ZipFile(zipFilePath).addFile(filePath); - logger.debug("Successfully zipped file {} to zip file {}", filePath, zipFilePath); - } - catch (ZipException e) { - logger.error(String.format("Error on zipping file %s to zip file %s", filePath, zipFilePath), e ); - - throw new RuntimeException(e); - } - } -} diff --git a/kgstatsSrv/src/main/kotlin/ru/klavogonki/statistics/zip/ZipUtils.kt b/kgstatsSrv/src/main/kotlin/ru/klavogonki/statistics/zip/ZipUtils.kt new file mode 100644 index 000000000..89e6508e7 --- /dev/null +++ b/kgstatsSrv/src/main/kotlin/ru/klavogonki/statistics/zip/ZipUtils.kt @@ -0,0 +1,23 @@ +package ru.klavogonki.statistics.zip + +import net.lingala.zip4j.ZipFile +import net.lingala.zip4j.exception.ZipException +import org.apache.logging.log4j.kotlin.Logging + +object ZipUtils : Logging { + + @JvmStatic + fun zipFile(filePath: String, zipFilePath: String) { + ZipFile(zipFilePath).use { + try { + it.addFile(filePath) + + logger.debug("Successfully zipped file \"$filePath\" to zip file \"$zipFilePath\".") + } catch (e: ZipException) { + logger.error("Error on zipping file \"$filePath\" to zip file \"$zipFilePath\"", e) + + throw e + } + } + } +} \ No newline at end of file