From a0c0639bbf9a0beedb07764697b3da20b8b0e3e6 Mon Sep 17 00:00:00 2001 From: EdwinBetanc0urt Date: Fri, 23 Feb 2024 21:06:31 -0400 Subject: [PATCH] change to vavr.io.control.Try. --- .../src/org/adempiere/pipo/CreateZipFile.java | 42 ++++++++++--------- 1 file changed, 22 insertions(+), 20 deletions(-) diff --git a/base/src/org/adempiere/pipo/CreateZipFile.java b/base/src/org/adempiere/pipo/CreateZipFile.java index 29f00727e0..3953f903bb 100644 --- a/base/src/org/adempiere/pipo/CreateZipFile.java +++ b/base/src/org/adempiere/pipo/CreateZipFile.java @@ -17,6 +17,7 @@ package org.adempiere.pipo; +import io.vavr.control.Try; import java.io.File; import java.io.IOException; import java.util.Enumeration; @@ -29,6 +30,7 @@ import org.apache.tools.ant.taskdefs.GZip; import org.apache.tools.ant.taskdefs.Tar; import org.apache.tools.ant.taskdefs.Zip; + /** * Compress package * @@ -100,28 +102,28 @@ static public void unpackFile(File zipFilepath, File destinationDir) Unzipper.execute(); } + static public String getParentDir(File zipFilepath) throws IOException { - String fileName = ""; - ZipFile zipFile = null; - try { - zipFile = new ZipFile(zipFilepath); - Enumeration entries = zipFile.entries(); - ZipEntry entry = entries.nextElement(); - File tempfile = new File(entry.getName()); - while (tempfile.getParent()!=null) { - tempfile = tempfile.getParentFile(); - } - fileName = tempfile.getName(); - } catch (IOException ioe) { - System.err.println("Unhandled exception:"); - ioe.printStackTrace(); - } finally { - if (zipFile != null) { - zipFile.close(); - } - } - return fileName; + Try result = Try.withResources(() -> { + return new ZipFile(zipFilepath); + }) + .of(zipFile -> { + Enumeration entries = zipFile.entries(); + ZipEntry entry = entries.nextElement(); + File tempfile = new File(entry.getName()); + while (tempfile.getParent()!=null) { + tempfile = tempfile.getParentFile(); + } + return tempfile.getName(); + }) + .onFailure(ioError -> { + System.err.println("Error reading file: " + ioError.getMessage()); + ioError.printStackTrace(); + }) + ; + + return result.get(); } } // CreateZipFile