Implemented : Add new FileUtil methods for zip management (OFBIZ-10188)

Add zipFileStream method that will zip FileStream to a zipped one.
Add unzipFileToFolder method to unzip a given archive to a given Folder.
Add fileUtil groovy test suite that create zip file containing README.md, then unzip it to tmp folder and compare original and unzipped files.
Thanks Jacques for the review.

git-svn-id: https://svn.apache.org/repos/asf/ofbiz/ofbiz-framework/trunk@1823567 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Gil Portenseigne
2018-02-08 15:32:31 +00:00
parent f5ba435024
commit fba4f5b235
3 changed files with 152 additions and 0 deletions
@@ -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))
}
}
@@ -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();
}
}
+1
View File
@@ -44,5 +44,6 @@
<junit-test-suite class-name="org.apache.ofbiz.base.util.test.UtilPropertiesTests"/>
<junit-test-suite class-name="org.apache.ofbiz.base.util.test.UtilXmlTests"/>
<groovy-test-suite name="simple" location="component://base/groovyScript/test/SimpleTests.groovy"/>
<groovy-test-suite name="fileUtil" location="component://base/groovyScript/test/FileUtilTests.groovy"/>
</test-group>
</test-suite>