diff --git a/framework/base/groovyScript/test/FileUtilTests.groovy b/framework/base/groovyScript/test/FileUtilTests.groovy
new file mode 100644
index 0000000000..fcaaf35efb
--- /dev/null
+++ b/framework/base/groovyScript/test/FileUtilTests.groovy
@@ -0,0 +1,64 @@
+/*******************************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *******************************************************************************/
+
+
+import org.apache.commons.io.FileUtils
+import org.apache.ofbiz.base.util.FileUtil
+import org.apache.ofbiz.base.util.UtilProperties
+import org.apache.ofbiz.testtools.GroovyScriptTestCase
+
+class FileUtilTests extends GroovyScriptTestCase {
+
+ /**
+ * Test FileUtil zipFileStream and unzipFileToFolder methods, using README.md
+ */
+ void testZipReadme() {
+ String zipFilePath = UtilProperties.getPropertyValue("general", "http.upload.tmprepository", "runtime/tmp")
+ String zipName = 'README.md.zip'
+ String fileName = 'README.md'
+ File originalReadme = new File(fileName)
+
+ //validate zipStream from README.md is not null
+ def zipStream = FileUtil.zipFileStream(originalReadme.newInputStream(), fileName)
+ assert zipStream
+
+ //ensure no zip already exists
+ File readmeZipped = new File(zipFilePath, zipName)
+ if (readmeZipped.exists()) readmeZipped.delete()
+
+ //write it down into tmp folder
+ OutputStream out = new FileOutputStream(readmeZipped)
+ byte[] buf = new byte[8192]
+ int len
+ while ((len = zipStream.read(buf)) > 0) {
+ out.write(buf, 0, len)
+ }
+ out.close()
+ zipStream.close()
+
+ //ensure no README.md exist in tmp folder
+ File readme = new File(zipFilePath, fileName)
+ if (readme.exists()) readme.delete()
+
+ //validate unzip and compare the two files
+ FileUtil.unzipFileToFolder(readmeZipped, zipFilePath)
+
+ assert FileUtils.contentEquals(originalReadme, new File(zipFilePath, fileName))
+ }
+}
diff --git a/framework/base/src/main/java/org/apache/ofbiz/base/util/FileUtil.java b/framework/base/src/main/java/org/apache/ofbiz/base/util/FileUtil.java
index 3193bdb8c9..0d1985f648 100644
--- a/framework/base/src/main/java/org/apache/ofbiz/base/util/FileUtil.java
+++ b/framework/base/src/main/java/org/apache/ofbiz/base/util/FileUtil.java
@@ -21,12 +21,14 @@ package org.apache.ofbiz.base.util;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
+import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.io.IOException;
+import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
@@ -38,6 +40,11 @@ import java.util.LinkedList;
import java.util.List;
import java.util.Set;
+import java.util.UUID;
+import java.util.zip.Deflater;
+import java.util.zip.ZipEntry;
+import java.util.zip.ZipInputStream;
+import java.util.zip.ZipOutputStream;
import org.apache.commons.io.FileUtils;
import org.apache.ofbiz.base.location.ComponentLocationResolver;
@@ -401,4 +408,84 @@ public final class FileUtil {
File f = new File(fileName);
return f.isFile();
}
+
+ /**
+ * For an inputStream and a file name, create a zip stream containing only one entry with the inputStream set to fileName
+ * If fileName is empty, generate a unique one.
+ *
+ * @param fileStream
+ * @param fileName
+ * @return
+ * @throws IOException
+ */
+ public static ByteArrayInputStream zipFileStream(InputStream fileStream, String fileName) throws IOException {
+ if (fileStream == null) return null;
+ if (fileName == null) fileName = UUID.randomUUID().toString();
+ // Create zip file from content input stream
+ String zipFileName = UUID.randomUUID().toString() + ".zip";
+ String zipFilePath = UtilProperties.getPropertyValue("general", "http.upload.tmprepository", "runtime/tmp");
+ FileOutputStream fos = new FileOutputStream(new File(zipFilePath, zipFileName));
+ ZipOutputStream zos = new ZipOutputStream(fos);
+ zos.setMethod(ZipOutputStream.DEFLATED);
+ zos.setLevel(Deflater.BEST_COMPRESSION);
+ ZipEntry ze = new ZipEntry(fileName);
+ zos.putNextEntry(ze);
+ int len;
+ byte[] bufferData = new byte[8192];
+ while ((len = fileStream.read(bufferData)) > 0) {
+ zos.write(bufferData, 0, len);
+ }
+ zos.closeEntry();
+ zos.close();
+ fos.close();
+
+ //prepare zip stream
+ File tmpZipFile = new File(zipFilePath, zipFileName);
+ ByteArrayInputStream zipStream = new ByteArrayInputStream(FileUtils.readFileToByteArray(tmpZipFile));
+
+ //Then delete zip file
+ tmpZipFile.delete();
+ return zipStream;
+ }
+
+ /**
+ * Unzip file structure of the given zipFile to specified outputFolder
+ *
+ * @param zipFile
+ * @param outputFolder
+ * @throws IOException
+ */
+ public static void unzipFileToFolder(File zipFile, String outputFolder) throws IOException {
+ byte[] buffer = new byte[8192];
+
+ //create output directory if not exists
+ File folder = new File(outputFolder);
+ if (!folder.exists()) {
+ folder.mkdir();
+ }
+
+ //get the zip file content
+ ZipInputStream zis = new ZipInputStream(new FileInputStream(zipFile));
+ //get the zipped file list entry
+ ZipEntry ze = zis.getNextEntry();
+
+ while (ze != null) {
+ String fileName = ze.getName();
+ File newFile = new File(outputFolder, fileName);
+
+ //create all non existing folders
+ //else you will hit FileNotFoundException for compressed folder
+ new File(newFile.getParent()).mkdirs();
+
+ FileOutputStream fos = new FileOutputStream(newFile);
+ int len;
+ while ((len = zis.read(buffer)) > 0) {
+ fos.write(buffer, 0, len);
+ }
+ fos.close();
+ ze = zis.getNextEntry();
+ }
+ zis.closeEntry();
+ zis.close();
+ }
}
diff --git a/framework/base/testdef/basetests.xml b/framework/base/testdef/basetests.xml
index 165715f598..5318c1d6b9 100644
--- a/framework/base/testdef/basetests.xml
+++ b/framework/base/testdef/basetests.xml
@@ -44,5 +44,6 @@
+