From 1d2f8a96eb0d059a457f6cb5bccf40ce5a381b37 Mon Sep 17 00:00:00 2001 From: Larry L Date: Fri, 16 Feb 2024 09:20:22 -0600 Subject: [PATCH] add unzipFile function --- .../mcaps/core/workflows/ScormExtractor.java | 36 +++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/core/src/main/java/com/mcaps/core/workflows/ScormExtractor.java b/core/src/main/java/com/mcaps/core/workflows/ScormExtractor.java index fa8e0a6..5698dc4 100644 --- a/core/src/main/java/com/mcaps/core/workflows/ScormExtractor.java +++ b/core/src/main/java/com/mcaps/core/workflows/ScormExtractor.java @@ -1,9 +1,15 @@ package com.mcaps.core.workflows; import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; import java.io.InputStream; import java.util.HashMap; import java.util.Map; +import java.util.zip.ZipEntry; +import java.util.zip.ZipInputStream; import javax.jcr.Node; import javax.jcr.Session; @@ -40,14 +46,15 @@ public class ScormExtractor implements WorkflowProcess { @Override public void execute(WorkItem arg0, WorkflowSession arg1, MetaDataMap arg2) throws WorkflowException { - log.warn("The string I got was ..." + arg2.get("PROCESS_ARGS", "string").toString()); + log.warn("SCORM WORKFLOW: MetaDataMap args: " + arg2.get("PROCESS_ARGS", "string").toString()); String[] params = arg2.get("PROCESS_ARGS", "string").toString().split(","); String attachmentsPath = params[0]; String saveToLocation = params[1]; - log.warn("The payload path is " + arg0.getContentPath()); + log.warn("SCORM WORKFLOW: payload path: " + arg0.getContentPath()); String payloadPath = arg0.getWorkflowData().getPayload().toString(); Map map = new HashMap (); map.put("path", payloadPath + "/" + attachmentsPath); + unzipFile(saveToLocation, payloadPath); //File saveLocationFolder = new File(saveToLocation); //if (!saveLocationFolder.exists()) { // saveLocationFolder.mkdirs(); @@ -56,4 +63,29 @@ public class ScormExtractor implements WorkflowProcess { map.put("type", "nt:file"); //throw new UnsupportedOperationException("Unimplemented method 'execute'"); } + + private void unzipFile(String filePath, String outputFolder) { + try (ZipInputStream zipIn = new ZipInputStream(new FileInputStream(filePath))) { + ZipEntry entry = zipIn.getNextEntry(); + while (entry != null) { + String zipFilePath = outputFolder + File.separator + entry.getName(); + log.warn("SCORM WORKFLOW: zipFilePath: " + zipFilePath); + if (!entry.isDirectory()) { + try (FileOutputStream fos = new FileOutputStream(filePath)) { + byte[] buffer = new byte[1024]; + int length; + while ((length = zipIn.read(buffer)) >= 0) { + fos.write(buffer, 0, length); + } + } + } + zipIn.closeEntry(); + entry = zipIn.getNextEntry(); + } + } catch (FileNotFoundException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + } } \ No newline at end of file