Skip to content

Commit

Permalink
Merge pull request #10 from maybeec/development
Browse files Browse the repository at this point in the history
#7 Enabling tar.gz support
  • Loading branch information
themetalone authored Oct 14, 2016
2 parents bd5e29b + 611b19d commit e9399fd
Show file tree
Hide file tree
Showing 9 changed files with 296 additions and 73 deletions.
5 changes: 3 additions & 2 deletions task-unzip/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ 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.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
2 changes: 1 addition & 1 deletion task-unzip/build.properties
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ bin.includes = .,\
META-INF/,\
plugin.xml,\
plugin.properties,\
lib/zip4j-1.3.2.jar
lib/commons-compress-1.12.jar
jars.compile.order = .
source.. = src/
output.. = bin/
12 changes: 7 additions & 5 deletions task-unzip/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@
</parent>

<dependencies>
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-compress -->
<dependency>
<groupId>net.lingala.zip4j</groupId>
<artifactId>zip4j</artifactId>
<version>1.3.2</version>
<groupId>org.apache.commons</groupId>
<artifactId>commons-compress</artifactId>
<version>1.12</version>
</dependency>

</dependencies>

<build>
Expand Down Expand Up @@ -77,8 +79,8 @@
</execution>
</executions>
</plugin>
<!-- Maven Build -->

<!-- Maven Build -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -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
}

}
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
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.UnzipTaskException;

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
Expand All @@ -23,87 +32,87 @@ public void unzip(String zipFile, String destDir) throws UnzipTaskException
SetupTaskLogger.getLogger().log("Archive: " + zipFile);
SetupTaskLogger.getLogger().log("Destination: " + destDir);

ZipFile zip;
File file = new File(zipFile);
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)
InputStream fileIS = new BufferedInputStream(new FileInputStream(file));
switch (archiveType(file.getName()))
{
SetupTaskLogger.getLogger().log(progress.getPercentDone() + " percent done");
try
case UNKNOWN:
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)))
{
Thread.sleep(500);
os.write(buffer, 0, n);
}
catch (Exception e)
os.close();
cIS.close();
fileIS.close();
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);
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();
out.close();
}
archiveIS.close();
SetupTaskLogger.getLogger().logInfo("Done");
}
}
catch (Exception e)
catch (Exception ex)
{
throw new UnzipTaskException("Could not unzip properly: " + e.getMessage());
throw new UnzipTaskException(ex);
}

}

}

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;
private ArchiveType archiveType(String fileName)
{
int lastDotPosition = fileName.lastIndexOf('.');
if (lastDotPosition == -1)
{
return ArchiveType.UNKNOWN;
}
String ending = fileName.substring(lastDotPosition + 1);

/**
* What went wrong
*/
String errorMessage;
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 ArchiveType.ARCHIVE;
}
if (ending.equals("bz2") || ending.equals("lzma") || ending.equals("xz") || ending.equals("pack200") || ending.equals("z") || ending.equals("gz"))
{
return ArchiveType.COMPRESSED;
}
return ArchiveType.UNKNOWN;

/**
* 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()
enum ArchiveType
{

ARCHIVE, COMPRESSED, UNKNOWN
}

}
Loading

0 comments on commit e9399fd

Please sign in to comment.