From 48eb829df2063508d4d989a0563fd6c437b0aa23 Mon Sep 17 00:00:00 2001 From: Steffen Holzer Date: Wed, 12 Oct 2016 16:28:42 +0200 Subject: [PATCH 1/4] #7 changed the used unzip library, added a new internal exception type and adapted the plugin configuration files --- task-unzip/META-INF/MANIFEST.MF | 8 +- task-unzip/build.properties | 8 +- task-unzip/pom.xml | 241 +++++++++--------- .../UnsupportedFileTypeException.java | 61 +++++ .../task/unzip/core/impl/UnzipUtilImpl.java | 108 +++----- 5 files changed, 230 insertions(+), 196 deletions(-) create mode 100644 task-unzip/src/com/github/maybeec/oomph/task/unzip/core/exceptions/UnsupportedFileTypeException.java diff --git a/task-unzip/META-INF/MANIFEST.MF b/task-unzip/META-INF/MANIFEST.MF index 5746dac..76b75e6 100644 --- a/task-unzip/META-INF/MANIFEST.MF +++ b/task-unzip/META-INF/MANIFEST.MF @@ -5,7 +5,13 @@ Bundle-Vendor: %providerName Bundle-SymbolicName: com.github.maybeec.oomph.task.unzip;singleton:=true Bundle-Version: 1.0.0.qualifier Bundle-ClassPath: ., - lib/zip4j-1.3.2.jar + lib/commons-compress-1.11.jar, + lib/commons-io-2.5.jar, + lib/plexus-archiver-3.4.jar, + lib/plexus-io-2.7.1.jar, + lib/plexus-utils-3.0.24.jar, + lib/snappy-0.4.jar, + lib/xz-1.5.jar Bundle-ActivationPolicy: lazy Bundle-Localization: plugin Bundle-RequiredExecutionEnvironment: J2SE-1.5 diff --git a/task-unzip/build.properties b/task-unzip/build.properties index a901266..1653e24 100644 --- a/task-unzip/build.properties +++ b/task-unzip/build.properties @@ -5,7 +5,13 @@ bin.includes = .,\ META-INF/,\ plugin.xml,\ plugin.properties,\ - lib/zip4j-1.3.2.jar + lib/commons-compress-1.11.jar,\ + lib/commons-io-2.5.jar,\ + lib/plexus-archiver-3.4.jar,\ + lib/plexus-io-2.7.1.jar,\ + lib/plexus-utils-3.0.24.jar,\ + lib/snappy-0.4.jar,\ + lib/xz-1.5.jar jars.compile.order = . source.. = src/ output.. = bin/ diff --git a/task-unzip/pom.xml b/task-unzip/pom.xml index df9be46..0ec6215 100644 --- a/task-unzip/pom.xml +++ b/task-unzip/pom.xml @@ -1,127 +1,128 @@ - - 4.0.0 - com.github.maybeec.oomph.task.unzip - Oomph Task - Unzip - eclipse-plugin + + 4.0.0 + com.github.maybeec.oomph.task.unzip + Oomph Task - Unzip + eclipse-plugin - - com.github.maybeec.oomph - task-unzip-parent - 1.0.0-SNAPSHOT - .. - + + com.github.maybeec.oomph + task-unzip-parent + 1.0.0-SNAPSHOT + .. + - - - net.lingala.zip4j - zip4j - 1.3.2 - - + + + org.codehaus.plexus + plexus-archiver + 3.4 + + - - - - - org.eclipse.tycho - tycho-maven-plugin - ${tycho.version} - true - + + + + + org.eclipse.tycho + tycho-maven-plugin + ${tycho.version} + true + - - org.eclipse.tycho - target-platform-configuration - - - - linux - gtk - x86 - - - linux - gtk - x86_64 - - - win32 - win32 - x86 - - - win32 - win32 - x86_64 - - - macosx - cocoa - x86_64 - - - - + + org.eclipse.tycho + target-platform-configuration + + + + linux + gtk + x86 + + + linux + gtk + x86_64 + + + win32 + win32 + x86 + + + win32 + win32 + x86_64 + + + macosx + cocoa + x86_64 + + + + - - org.eclipse.tycho - tycho-source-plugin - - - plugin-source - - plugin-source - - - - - - - - org.apache.maven.plugins - maven-dependency-plugin - - - copy-dependencies - initialize - - copy-dependencies - - - runtime - true - ${project.basedir}/lib - ${project.basedir}/lib - - - - - - org.apache.maven.plugins - maven-clean-plugin - 2.5 - - - clean-dependencies - clean - - clean - - - - - lib - - **/* - - - - - - - - - + + org.eclipse.tycho + tycho-source-plugin + + + plugin-source + + plugin-source + + + + + + + + org.apache.maven.plugins + maven-dependency-plugin + + + copy-dependencies + initialize + + copy-dependencies + + + runtime + true + ${project.basedir}/lib + ${project.basedir}/lib + + + + + + org.apache.maven.plugins + maven-clean-plugin + 2.5 + + + clean-dependencies + clean + + clean + + + + + lib + + **/* + + + + + + + + + diff --git a/task-unzip/src/com/github/maybeec/oomph/task/unzip/core/exceptions/UnsupportedFileTypeException.java b/task-unzip/src/com/github/maybeec/oomph/task/unzip/core/exceptions/UnsupportedFileTypeException.java new file mode 100644 index 0000000..1fc7cef --- /dev/null +++ b/task-unzip/src/com/github/maybeec/oomph/task/unzip/core/exceptions/UnsupportedFileTypeException.java @@ -0,0 +1,61 @@ +/** + * + */ +package com.github.maybeec.oomph.task.unzip.core.exceptions; + +/** + * @author sholzer + * + */ +public class UnsupportedFileTypeException extends UnzipTaskException +{ + + /** + * + */ + public UnsupportedFileTypeException() + { + // TODO Auto-generated constructor stub + } + + /** + * @param message + */ + public UnsupportedFileTypeException(String message) + { + super(message); + // TODO Auto-generated constructor stub + } + + /** + * @param cause + */ + public UnsupportedFileTypeException(Throwable cause) + { + super(cause); + // TODO Auto-generated constructor stub + } + + /** + * @param message + * @param cause + */ + public UnsupportedFileTypeException(String message, Throwable cause) + { + super(message, cause); + // TODO Auto-generated constructor stub + } + + /** + * @param message + * @param cause + * @param enableSuppression + * @param writableStackTrace + */ + public UnsupportedFileTypeException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) + { + super(message, cause, enableSuppression, writableStackTrace); + // TODO Auto-generated constructor stub + } + +} diff --git a/task-unzip/src/com/github/maybeec/oomph/task/unzip/core/impl/UnzipUtilImpl.java b/task-unzip/src/com/github/maybeec/oomph/task/unzip/core/impl/UnzipUtilImpl.java index f02b76c..3fa921a 100644 --- a/task-unzip/src/com/github/maybeec/oomph/task/unzip/core/impl/UnzipUtilImpl.java +++ b/task-unzip/src/com/github/maybeec/oomph/task/unzip/core/impl/UnzipUtilImpl.java @@ -1,12 +1,14 @@ package com.github.maybeec.oomph.task.unzip.core.impl; -import net.lingala.zip4j.core.ZipFile; -import net.lingala.zip4j.progress.ProgressMonitor; - import com.github.maybeec.oomph.task.unzip.core.SetupTaskLogger; import com.github.maybeec.oomph.task.unzip.core.UnzipUtil; +import com.github.maybeec.oomph.task.unzip.core.exceptions.UnsupportedFileTypeException; import com.github.maybeec.oomph.task.unzip.core.exceptions.UnzipTaskException; +import org.codehaus.plexus.archiver.UnArchiver; +import org.codehaus.plexus.archiver.tar.TarGZipUnArchiver; +import org.codehaus.plexus.archiver.zip.ZipUnArchiver; + import java.io.File; /** @@ -23,87 +25,45 @@ public void unzip(String zipFile, String destDir) throws UnzipTaskException SetupTaskLogger.getLogger().log("Archive: " + zipFile); SetupTaskLogger.getLogger().log("Destination: " + destDir); - ZipFile zip; + UnArchiver unarchiver = getUnArchiver(zipFile); + unarchiver.setDestFile(new File(zipFile)); + unarchiver.setDestDirectory(new File(destDir)); + try { - zip = new ZipFile(new File(zipFile)); - } - catch (Exception ex) - { - throw new UnzipTaskException("Encountered problems :" + ex.getMessage()); - } - try - { - zip.setRunInThread(true); - ProgressMonitor progress = zip.getProgressMonitor(); - zip.extractAll(destDir); - while (progress.getState() == ProgressMonitor.STATE_BUSY) - { - SetupTaskLogger.getLogger().log(progress.getPercentDone() + " percent done"); - try - { - Thread.sleep(500); - } - catch (Exception e) - { - } - } + unarchiver.extract(); } catch (Exception e) { - throw new UnzipTaskException("Could not unzip properly: " + e.getMessage()); + throw new UnzipTaskException("Could not unzip properly: " + e.getMessage(), e); } } -} - -class UnzipThread extends Thread -{ - - /** - * zip4j api - */ - ZipFile zip; - - /** - * destination directory - */ - String destination; - - /** - * if something went wrong. Since I cannot throw exceptions through the runnable interface I used this - * work around - */ - boolean error = false; - - /** - * What went wrong - */ - String errorMessage; - - /** - * Constructor - * @param zip - * the zip4j api obj - * @param destination - * destination directory - * @author sholzer (Feb 10, 2016) - */ - UnzipThread(ZipFile zip, String destination) - { - this.zip = zip; - this.destination = destination; - } - - /** - * {@inheritDoc} - * @author sholzer (Feb 10, 2016) - */ - @Override - public void run() + private UnArchiver getUnArchiver(String zipFile) throws UnsupportedFileTypeException { - + String fileExtension = "undefined"; + try + { + int lastDot = zipFile.lastIndexOf('.'); + fileExtension = zipFile.substring(lastDot + 1); + } + catch (IllegalArgumentException e) + { + throw new UnsupportedFileTypeException("Could not find an unarchiver for file type " + fileExtension, e); + } + if (fileExtension.equals("gz")) + { + return new TarGZipUnArchiver(); + } + else + { + if (!fileExtension.equals("zip")) + { + SetupTaskLogger.getLogger().logWarning("Could not find Unarchiver for " + fileExtension + " ! Trying Zip Unarchiver. "); + } + return new ZipUnArchiver(); + } } } From c99550b9dd3fed5804ce7c240cc3055193b8a4ab Mon Sep 17 00:00:00 2001 From: Steffen Holzer Date: Fri, 14 Oct 2016 16:26:45 +0200 Subject: [PATCH 2/4] #7 using the apache commons now --- task-unzip/META-INF/MANIFEST.MF | 11 +- task-unzip/build.properties | 8 +- task-unzip/pom.xml | 241 +++++++++--------- .../task/unzip/core/impl/UnzipUtilImpl.java | 96 ++++--- .../task/unzip/core/impl/UnzipUtilTest.java | 38 +++ 5 files changed, 229 insertions(+), 165 deletions(-) create mode 100644 task-unzip/test/com/github/maybeec/oomph/task/unzip/core/impl/UnzipUtilTest.java diff --git a/task-unzip/META-INF/MANIFEST.MF b/task-unzip/META-INF/MANIFEST.MF index 76b75e6..02dad0c 100644 --- a/task-unzip/META-INF/MANIFEST.MF +++ b/task-unzip/META-INF/MANIFEST.MF @@ -5,20 +5,15 @@ Bundle-Vendor: %providerName Bundle-SymbolicName: com.github.maybeec.oomph.task.unzip;singleton:=true Bundle-Version: 1.0.0.qualifier Bundle-ClassPath: ., - lib/commons-compress-1.11.jar, - lib/commons-io-2.5.jar, - lib/plexus-archiver-3.4.jar, - lib/plexus-io-2.7.1.jar, - lib/plexus-utils-3.0.24.jar, - lib/snappy-0.4.jar, - lib/xz-1.5.jar + lib/commons-compress-1.12.jar Bundle-ActivationPolicy: lazy Bundle-Localization: plugin Bundle-RequiredExecutionEnvironment: J2SE-1.5 Require-Bundle: org.eclipse.core.runtime;bundle-version="[3.5.0,4.0.0)", org.eclipse.emf.ecore;bundle-version="[2.10.0,3.0.0)";visibility:=reexport, org.eclipse.oomph.setup;bundle-version="[1.1.0,2.0.0)";visibility:=reexport, - org.eclipse.oomph.base;bundle-version="[1.1.0,2.0.0)";visibility:=reexport + org.eclipse.oomph.base;bundle-version="[1.1.0,2.0.0)";visibility:=reexport, + org.junit Export-Package: com.github.maybeec.oomph.task.unzip, com.github.maybeec.oomph.task.unzip.impl, com.github.maybeec.oomph.task.unzip.util diff --git a/task-unzip/build.properties b/task-unzip/build.properties index 1653e24..d22e8c4 100644 --- a/task-unzip/build.properties +++ b/task-unzip/build.properties @@ -5,13 +5,7 @@ bin.includes = .,\ META-INF/,\ plugin.xml,\ plugin.properties,\ - lib/commons-compress-1.11.jar,\ - lib/commons-io-2.5.jar,\ - lib/plexus-archiver-3.4.jar,\ - lib/plexus-io-2.7.1.jar,\ - lib/plexus-utils-3.0.24.jar,\ - lib/snappy-0.4.jar,\ - lib/xz-1.5.jar + lib/commons-compress-1.12.jar jars.compile.order = . source.. = src/ output.. = bin/ diff --git a/task-unzip/pom.xml b/task-unzip/pom.xml index 0ec6215..c466935 100644 --- a/task-unzip/pom.xml +++ b/task-unzip/pom.xml @@ -1,128 +1,129 @@ - - 4.0.0 - com.github.maybeec.oomph.task.unzip - Oomph Task - Unzip - eclipse-plugin + + 4.0.0 + com.github.maybeec.oomph.task.unzip + Oomph Task - Unzip + eclipse-plugin - - com.github.maybeec.oomph - task-unzip-parent - 1.0.0-SNAPSHOT - .. - + + com.github.maybeec.oomph + task-unzip-parent + 1.0.0-SNAPSHOT + .. + - - - org.codehaus.plexus - plexus-archiver - 3.4 - - + + + + org.apache.commons + commons-compress + 1.12 + - - - - - org.eclipse.tycho - tycho-maven-plugin - ${tycho.version} - true - + - - org.eclipse.tycho - target-platform-configuration - - - - linux - gtk - x86 - - - linux - gtk - x86_64 - - - win32 - win32 - x86 - - - win32 - win32 - x86_64 - - - macosx - cocoa - x86_64 - - - - + + + + + org.eclipse.tycho + tycho-maven-plugin + ${tycho.version} + true + - - org.eclipse.tycho - tycho-source-plugin - - - plugin-source - - plugin-source - - - - + + org.eclipse.tycho + target-platform-configuration + + + + linux + gtk + x86 + + + linux + gtk + x86_64 + + + win32 + win32 + x86 + + + win32 + win32 + x86_64 + + + macosx + cocoa + x86_64 + + + + - - - org.apache.maven.plugins - maven-dependency-plugin - - - copy-dependencies - initialize - - copy-dependencies - - - runtime - true - ${project.basedir}/lib - ${project.basedir}/lib - - - - - - org.apache.maven.plugins - maven-clean-plugin - 2.5 - - - clean-dependencies - clean - - clean - - - - - lib - - **/* - - - - - - - - - + + org.eclipse.tycho + tycho-source-plugin + + + plugin-source + + plugin-source + + + + + + + + org.apache.maven.plugins + maven-dependency-plugin + + + copy-dependencies + initialize + + copy-dependencies + + + runtime + true + ${project.basedir}/lib + ${project.basedir}/lib + + + + + + org.apache.maven.plugins + maven-clean-plugin + 2.5 + + + clean-dependencies + clean + + clean + + + + + lib + + **/* + + + + + + + + + diff --git a/task-unzip/src/com/github/maybeec/oomph/task/unzip/core/impl/UnzipUtilImpl.java b/task-unzip/src/com/github/maybeec/oomph/task/unzip/core/impl/UnzipUtilImpl.java index 3fa921a..bb1b53d 100644 --- a/task-unzip/src/com/github/maybeec/oomph/task/unzip/core/impl/UnzipUtilImpl.java +++ b/task-unzip/src/com/github/maybeec/oomph/task/unzip/core/impl/UnzipUtilImpl.java @@ -2,14 +2,21 @@ import com.github.maybeec.oomph.task.unzip.core.SetupTaskLogger; import com.github.maybeec.oomph.task.unzip.core.UnzipUtil; -import com.github.maybeec.oomph.task.unzip.core.exceptions.UnsupportedFileTypeException; import com.github.maybeec.oomph.task.unzip.core.exceptions.UnzipTaskException; -import org.codehaus.plexus.archiver.UnArchiver; -import org.codehaus.plexus.archiver.tar.TarGZipUnArchiver; -import org.codehaus.plexus.archiver.zip.ZipUnArchiver; +import org.apache.commons.compress.archivers.ArchiveEntry; +import org.apache.commons.compress.archivers.ArchiveInputStream; +import org.apache.commons.compress.archivers.ArchiveStreamFactory; +import org.apache.commons.compress.compressors.CompressorInputStream; +import org.apache.commons.compress.compressors.CompressorStreamFactory; +import org.apache.commons.compress.utils.IOUtils; +import java.io.BufferedInputStream; import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.InputStream; +import java.io.OutputStream; /** * @author sholzer @@ -25,45 +32,74 @@ public void unzip(String zipFile, String destDir) throws UnzipTaskException SetupTaskLogger.getLogger().log("Archive: " + zipFile); SetupTaskLogger.getLogger().log("Destination: " + destDir); - UnArchiver unarchiver = getUnArchiver(zipFile); - unarchiver.setDestFile(new File(zipFile)); - unarchiver.setDestDirectory(new File(destDir)); - + File file = new File(zipFile); try { - unarchiver.extract(); + InputStream fileIS = new FileInputStream(file); + switch (archiveType(file.getName())) + { + case UNKOWN: + SetupTaskLogger.getLogger().logWarning("Unknown archive extension of " + file.getName() + ". Trying nevertheless"); + //$FALL-THROUGH$ + case COMPRESSED: + SetupTaskLogger.getLogger().log("Decompressing Archive"); + BufferedInputStream in = new BufferedInputStream(fileIS); + CompressorInputStream cIS = new CompressorStreamFactory().createCompressorInputStream(in); + File tempFile = File.createTempFile(file.getName(), ".decompressed"); + OutputStream os = new FileOutputStream(tempFile); + final byte[] buffer = new byte[256]; + int n = 0; + while (-1 != (n = cIS.read(buffer))) + { + os.write(buffer, 0, n); + } + os.close(); + cIS.close(); + fileIS.close(); + fileIS = new FileInputStream(tempFile); + //$FALL-THROUGH$ + case ARCHIVE: + SetupTaskLogger.getLogger().log("Unarchiving"); + ArchiveInputStream archiveIS = new ArchiveStreamFactory().createArchiveInputStream(fileIS); + while (archiveIS.available() != 0) + { + ArchiveEntry entry = archiveIS.getNextEntry(); + OutputStream out = new FileOutputStream(new File(destDir, entry.getName())); + IOUtils.copy(archiveIS, out); + out.close(); + } + archiveIS.close(); + } } - catch (Exception e) + catch (Exception ex) { - throw new UnzipTaskException("Could not unzip properly: " + e.getMessage(), e); + throw new UnzipTaskException(ex); } } - private UnArchiver getUnArchiver(String zipFile) throws UnsupportedFileTypeException + private ArchiveType archiveType(String fileName) { - String fileExtension = "undefined"; - try - { - int lastDot = zipFile.lastIndexOf('.'); - fileExtension = zipFile.substring(lastDot + 1); - } - catch (IllegalArgumentException e) - { - throw new UnsupportedFileTypeException("Could not find an unarchiver for file type " + fileExtension, e); - } - if (fileExtension.equals("gz")) + + String[] components = fileName.split("."); + String ending = components[components.length - 1].toLowerCase(); + + if (ending.equals("zip") || ending.equals("cpio") || ending.equals("ar") || ending.equals("tar") || ending.equals("jar") || ending.equals("dump") + || ending.equals("7z") || ending.equals("arj")) { - return new TarGZipUnArchiver(); + return ArchiveType.ARCHIVE; } - else + if (ending.equals("bz2") || ending.equals("lzma") || ending.equals("xz") || ending.equals("pack200") || ending.equals("z") || ending.equals("gz")) { - if (!fileExtension.equals("zip")) - { - SetupTaskLogger.getLogger().logWarning("Could not find Unarchiver for " + fileExtension + " ! Trying Zip Unarchiver. "); - } - return new ZipUnArchiver(); + return ArchiveType.COMPRESSED; } + return ArchiveType.UNKOWN; + + } + + enum ArchiveType + { + ARCHIVE, COMPRESSED, UNKOWN } } diff --git a/task-unzip/test/com/github/maybeec/oomph/task/unzip/core/impl/UnzipUtilTest.java b/task-unzip/test/com/github/maybeec/oomph/task/unzip/core/impl/UnzipUtilTest.java new file mode 100644 index 0000000..196481d --- /dev/null +++ b/task-unzip/test/com/github/maybeec/oomph/task/unzip/core/impl/UnzipUtilTest.java @@ -0,0 +1,38 @@ +package com.github.maybeec.oomph.task.unzip.core.impl; + +import com.github.maybeec.oomph.task.unzip.core.UnzipUtil; + +import org.junit.Before; +import org.junit.Test; + +/** + * + * @author sholzer (14.10.2016) + */ +public class UnzipUtilTest +{ + String resourceFolder = "./test/resources/"; + + String tarGzFileName = resourceFolder + "tar.tar.gz"; + + UnzipUtil util; + + @Before + public void before() + { + util = new UnzipUtilImpl(); + } + + @Test + public void testZip() + { + String zipFileName = resourceFolder + "zip.zip"; + + } + + public void testTarGZ() throws Exception + { + + } + +} From d33af570e94e5b6aca66d3ae54efabf274e62078 Mon Sep 17 00:00:00 2001 From: Steffen Holzer Date: Fri, 14 Oct 2016 17:35:08 +0200 Subject: [PATCH 3/4] #7 fixed the unzip process. added tests. tests passed --- .../task/unzip/core/impl/UnzipUtilImpl.java | 27 +++++--- .../task/unzip/core/impl/UnzipUtilTest.java | 65 ++++++++++++++++-- task-unzip/test/resources/tarfile.tar.gz | Bin 0 -> 154 bytes task-unzip/test/resources/zipfile.zip | Bin 0 -> 316 bytes 4 files changed, 77 insertions(+), 15 deletions(-) create mode 100644 task-unzip/test/resources/tarfile.tar.gz create mode 100644 task-unzip/test/resources/zipfile.zip diff --git a/task-unzip/src/com/github/maybeec/oomph/task/unzip/core/impl/UnzipUtilImpl.java b/task-unzip/src/com/github/maybeec/oomph/task/unzip/core/impl/UnzipUtilImpl.java index bb1b53d..224b6ad 100644 --- a/task-unzip/src/com/github/maybeec/oomph/task/unzip/core/impl/UnzipUtilImpl.java +++ b/task-unzip/src/com/github/maybeec/oomph/task/unzip/core/impl/UnzipUtilImpl.java @@ -35,10 +35,10 @@ public void unzip(String zipFile, String destDir) throws UnzipTaskException File file = new File(zipFile); try { - InputStream fileIS = new FileInputStream(file); + InputStream fileIS = new BufferedInputStream(new FileInputStream(file)); switch (archiveType(file.getName())) { - case UNKOWN: + case UNKNOWN: SetupTaskLogger.getLogger().logWarning("Unknown archive extension of " + file.getName() + ". Trying nevertheless"); //$FALL-THROUGH$ case COMPRESSED: @@ -56,19 +56,23 @@ public void unzip(String zipFile, String destDir) throws UnzipTaskException os.close(); cIS.close(); fileIS.close(); - fileIS = new FileInputStream(tempFile); + SetupTaskLogger.getLogger().log("Decompressing done"); + fileIS = new BufferedInputStream(new FileInputStream(tempFile)); //$FALL-THROUGH$ case ARCHIVE: SetupTaskLogger.getLogger().log("Unarchiving"); ArchiveInputStream archiveIS = new ArchiveStreamFactory().createArchiveInputStream(fileIS); - while (archiveIS.available() != 0) + ArchiveEntry entry; + while ((entry = archiveIS.getNextEntry()) != null) { - ArchiveEntry entry = archiveIS.getNextEntry(); + SetupTaskLogger.getLogger().log("Extracting " + entry.getName()); OutputStream out = new FileOutputStream(new File(destDir, entry.getName())); IOUtils.copy(archiveIS, out); + out.flush(); out.close(); } archiveIS.close(); + SetupTaskLogger.getLogger().logInfo("Done"); } } catch (Exception ex) @@ -80,9 +84,12 @@ public void unzip(String zipFile, String destDir) throws UnzipTaskException private ArchiveType archiveType(String fileName) { - - String[] components = fileName.split("."); - String ending = components[components.length - 1].toLowerCase(); + int lastDotPosition = fileName.lastIndexOf('.'); + if (lastDotPosition == -1) + { + return ArchiveType.UNKNOWN; + } + String ending = fileName.substring(lastDotPosition + 1); if (ending.equals("zip") || ending.equals("cpio") || ending.equals("ar") || ending.equals("tar") || ending.equals("jar") || ending.equals("dump") || ending.equals("7z") || ending.equals("arj")) @@ -93,13 +100,13 @@ private ArchiveType archiveType(String fileName) { return ArchiveType.COMPRESSED; } - return ArchiveType.UNKOWN; + return ArchiveType.UNKNOWN; } enum ArchiveType { - ARCHIVE, COMPRESSED, UNKOWN + ARCHIVE, COMPRESSED, UNKNOWN } } diff --git a/task-unzip/test/com/github/maybeec/oomph/task/unzip/core/impl/UnzipUtilTest.java b/task-unzip/test/com/github/maybeec/oomph/task/unzip/core/impl/UnzipUtilTest.java index 196481d..2fbbb8c 100644 --- a/task-unzip/test/com/github/maybeec/oomph/task/unzip/core/impl/UnzipUtilTest.java +++ b/task-unzip/test/com/github/maybeec/oomph/task/unzip/core/impl/UnzipUtilTest.java @@ -1,19 +1,27 @@ package com.github.maybeec.oomph.task.unzip.core.impl; +import static org.junit.Assert.assertTrue; + import com.github.maybeec.oomph.task.unzip.core.UnzipUtil; +import com.github.maybeec.oomph.task.unzip.core.exceptions.UnzipTaskException; +import org.junit.AfterClass; import org.junit.Before; import org.junit.Test; +import java.io.File; + /** * * @author sholzer (14.10.2016) */ public class UnzipUtilTest { - String resourceFolder = "./test/resources/"; + public static String resourceFolder = "./test/resources/"; - String tarGzFileName = resourceFolder + "tar.tar.gz"; + public static String destination = resourceFolder + "dest"; + + public static File dest = new File(destination); UnzipUtil util; @@ -24,15 +32,62 @@ public void before() } @Test - public void testZip() + public void testZip() throws UnzipTaskException { - String zipFileName = resourceFolder + "zip.zip"; - + String zipFileName = resourceFolder + "zipfile.zip"; + util.unzip(zipFileName, destination); + File[] files = dest.listFiles(); + boolean zippedExtracted = false; + boolean zippedCopyExtracted = false; + for (File f : files) + { + if (f.getName().equals("zipped.txt")) + { + zippedExtracted = true; + } + if (f.getName().equals("zipped - Copy.txt")) + { + zippedCopyExtracted = true; + } + } + assertTrue("zipped.txt not extracted", zippedExtracted); + assertTrue("zipped - Copy.txt not extracted", zippedCopyExtracted); } + @Test public void testTarGZ() throws Exception + { + String tarGzFileName = resourceFolder + "tarfile.tar.gz"; + util.unzip(tarGzFileName, destination); + File[] files = dest.listFiles(); + boolean taredExtracted = false; + boolean taredCopyExtracted = false; + for (File f : files) + { + if (f.getName().equals("tared.txt")) + { + taredExtracted = true; + } + if (f.getName().equals("tared - Copy.txt")) + { + taredCopyExtracted = true; + } + } + assertTrue("tared.txt not extracted", taredExtracted); + assertTrue("tared - Copy.txt not extracted", taredCopyExtracted); + } + + @AfterClass + public static void afterAll() { + for (File f : dest.listFiles()) + { + if (f.isFile()) + { + f.delete(); + } + } } } diff --git a/task-unzip/test/resources/tarfile.tar.gz b/task-unzip/test/resources/tarfile.tar.gz new file mode 100644 index 0000000000000000000000000000000000000000..0d5c2ee5078c1eeb7c3695169af05e4ac400c39e GIT binary patch literal 154 zcmV;L0A>FliwFqV?f_T>0CZt;W@&6?E_7jX0PWLD3WGolfZ-fvF3^(EX>x#`#8zA1 zijJVS_l3B1;~w^Aql4f~PRyBwlTKYdL{G1?T*NO1+YblJtzstn8fJy!?Uy zxJE;u#`l(e-fTb(APmwB(FoF!n3SB7nik;A$Yc+e!Df*PPzMMofJvBcBx|{#Qj81| z46S-5BARNK7^_64J1 Date: Fri, 14 Oct 2016 18:06:32 +0200 Subject: [PATCH 4/4] #7 tested and fixed nested folder problems --- .../task/unzip/core/impl/UnzipUtilImpl.java | 6 ++ .../task/unzip/core/impl/UnzipUtilTest.java | 57 ++++++++++++++++++ task-unzip/test/resources/nested.tar | Bin 0 -> 3072 bytes 3 files changed, 63 insertions(+) create mode 100644 task-unzip/test/resources/nested.tar diff --git a/task-unzip/src/com/github/maybeec/oomph/task/unzip/core/impl/UnzipUtilImpl.java b/task-unzip/src/com/github/maybeec/oomph/task/unzip/core/impl/UnzipUtilImpl.java index 224b6ad..053bff2 100644 --- a/task-unzip/src/com/github/maybeec/oomph/task/unzip/core/impl/UnzipUtilImpl.java +++ b/task-unzip/src/com/github/maybeec/oomph/task/unzip/core/impl/UnzipUtilImpl.java @@ -65,7 +65,13 @@ public void unzip(String zipFile, String destDir) throws UnzipTaskException ArchiveEntry entry; while ((entry = archiveIS.getNextEntry()) != null) { + SetupTaskLogger.getLogger().log("Extracting " + entry.getName()); + if (entry.isDirectory()) + { + new File(destDir, entry.getName()).mkdir(); + continue; + } OutputStream out = new FileOutputStream(new File(destDir, entry.getName())); IOUtils.copy(archiveIS, out); out.flush(); diff --git a/task-unzip/test/com/github/maybeec/oomph/task/unzip/core/impl/UnzipUtilTest.java b/task-unzip/test/com/github/maybeec/oomph/task/unzip/core/impl/UnzipUtilTest.java index 2fbbb8c..92940a6 100644 --- a/task-unzip/test/com/github/maybeec/oomph/task/unzip/core/impl/UnzipUtilTest.java +++ b/task-unzip/test/com/github/maybeec/oomph/task/unzip/core/impl/UnzipUtilTest.java @@ -77,6 +77,48 @@ public void testTarGZ() throws Exception assertTrue("tared - Copy.txt not extracted", taredCopyExtracted); } + @Test + public void testNestedFiles() throws Exception + { + String nestedFolders = resourceFolder + "nested.tar"; + util.unzip(nestedFolders, destination); + boolean firstLvlFolder = false; + boolean secondLvlFolder = false; + boolean secondLvlFile = false; + boolean thirdLvlFile = false; + + for (File f : dest.listFiles()) + { + if (f.isDirectory() && f.getName().equals("New folder")) + { + firstLvlFolder = true; + for (File ff : f.listFiles()) + { + if (ff.isDirectory() && ff.getName().equals("New folder")) + { + secondLvlFolder = true; + for (File fff : ff.listFiles()) + { + if (fff.getName().equals("test.txt")) + { + thirdLvlFile = true; + } + } + } + if (ff.isFile() && ff.getName().equals("test.txt")) + { + secondLvlFile = true; + } + } + } + } + + assertTrue("Problems in the first lvl", firstLvlFolder); + assertTrue("Problems in the second lvl", secondLvlFile && secondLvlFolder); + assertTrue("Problems in the third lvl", thirdLvlFile); + + } + @AfterClass public static void afterAll() { @@ -87,7 +129,22 @@ public static void afterAll() { f.delete(); } + else + { + deleteFolder(f); + } } } + public static void deleteFolder(File f) + { + for (File ff : f.listFiles()) + { + if (ff.isDirectory()) + { + deleteFolder(ff); + } + ff.delete(); + } + } } diff --git a/task-unzip/test/resources/nested.tar b/task-unzip/test/resources/nested.tar new file mode 100644 index 0000000000000000000000000000000000000000..e9fe23ac95ca16a9fb8e3d33609d515545fafb92 GIT binary patch literal 3072 zcmeH|K@P$o5Jfo(C$KXzFt_L(OvIImNsH0rJ8GJaO*bU88;1q`Oev86`rAG~{N;8% z=b>|3H2?x;cJOV>>$~bONRP}ELGH`!y&(S%0Rs3iwI%GWXN?H~%3C|3Dm_-zAfl|LObR7P@*#s`!)q WClDh_{*Avf(WE(G4wwUt9e4r1a!