Skip to content

Commit

Permalink
Added ability to pipe compress operations (#3).
Browse files Browse the repository at this point in the history
For example, if your source file is a .jar and dest is .jar.pack.xz, the compressor will invoke both the pack and xz providers.

TODO: Ability to pipe extractions.
  • Loading branch information
dscalzi committed Dec 13, 2018
1 parent 1703b3a commit 427aa74
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@

import java.io.File;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import com.dscalzi.zipextractor.core.managers.MessageManager;
import com.dscalzi.zipextractor.core.provider.TypeProvider;
Expand All @@ -37,36 +39,87 @@ public static void asyncCompress(ICommandSender sender, File src, File dest, boo
mm.sourceNotFound(sender, src.getAbsolutePath());
return;
}


String[] srcSplit = src.toPath().toAbsolutePath().normalize().toString().split("\\.", 2);
String[] destSplit = dest.toPath().toAbsolutePath().normalize().toString().split("\\.", 2);

// We need an extension.
if(destSplit.length < 2) {
mm.invalidCompressionExtension(sender);
return;
}

String[] srcExts = srcSplit.length > 1 ? srcSplit[1].split("\\.") : new String[0];
String[] destExts = destSplit[1].split("\\.");

Map<File, File> pMap = new LinkedHashMap<File, File>();
if(destExts.length < 2) {
pMap.put(src, dest);
} else {
File dTemp = dest;
File sTemp = src;
String extAcc = "";
for (int i=0; i<destExts.length; i++) {
extAcc += '.' + destExts[i];
if (i<srcExts.length && !srcExts[i].equals(destExts[i]) || !(i<srcExts.length)) {
dTemp = new File(destSplit[0] + extAcc);
pMap.put(sTemp, dTemp);
sTemp = dTemp;
}
}
}

Runnable task = null;
for (final TypeProvider p : TypeProvider.getProviders()) {
if (p.destValidForCompression(dest)) {
if (p.srcValidForCompression(src)) {
if (dest.exists() && !override) {
mm.destExists(sender);
int c = 0;
boolean piped = false;
final Runnable[] pipes = new Runnable[pMap.size()];
for (final Map.Entry<File, File> e : pMap.entrySet()) {
for (final TypeProvider p : TypeProvider.getProviders()) {
if (p.destValidForCompression(e.getValue())) {
if (p.srcValidForCompression(e.getKey())) {
if (e.getValue().exists() && !override) {
mm.destExists(sender);
return;
}
final boolean printFinish = c != pMap.size()-1;
if(piped) {
pipes[c] = () -> {
p.compress(sender, e.getKey(), e.getValue(), log, printFinish);
e.getKey().delete();
};
} else {
pipes[c] = () -> {
p.compress(sender, e.getKey(), e.getValue(), log, printFinish);
};
}
piped = true;
} else {
mm.invalidSourceForDest(sender, p.canCompressFrom(), p.canCompressTo());
return;
}
task = () -> {
p.compress(sender, src, dest, log);
};
} else {
mm.invalidSourceForDest(sender, p.canCompressFrom(), p.canCompressTo());
return;
}
}
// If we can't process this phase, cancel the operation.
if(pipes[c] == null) {
mm.invalidCompressionExtension(sender);
return;
}
c++;
}

task = () -> {
for(Runnable r : pipes) {
r.run();
}
};

if (task != null) {
int result = ZServicer.getInstance().submit(task);
if (result == 0)
mm.addToQueue(sender, ZServicer.getInstance().getSize());
else if (result == 1)
mm.queueFull(sender, ZServicer.getInstance().getMaxQueueSize());
else if (result == 2)
mm.executorTerminated(sender, ZTask.COMPRESS);
} else {
mm.invalidCompressionExtension(sender);
}
int result = ZServicer.getInstance().submit(task);
if (result == 0)
mm.addToQueue(sender, ZServicer.getInstance().getSize());
else if (result == 1)
mm.queueFull(sender, ZServicer.getInstance().getMaxQueueSize());
else if (result == 2)
mm.executorTerminated(sender, ZTask.COMPRESS);
}

public static List<String> supportedExtensions() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,15 @@ public void extract(ICommandSender sender, File src, File dest, boolean log) {
}

@Override
public void compress(ICommandSender sender, File src, File dest, boolean log) {
public void compress(ICommandSender sender, File src, File dest, boolean log, boolean pipe) {
final MessageManager mm = MessageManager.inst();
mm.startingProcess(sender, ZTask.COMPRESS, src.getName());
try (JarFile in = new JarFile(src); OutputStream out = Files.newOutputStream(dest.toPath())) {
if (log)
mm.info("Compressing : " + src.getAbsolutePath());
Pack200.newPacker().pack(in, out);
mm.compressionComplete(sender, dest.getAbsolutePath());
if(!pipe)
mm.compressionComplete(sender, dest.getAbsolutePath());
} catch (IOException e) {
e.printStackTrace();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,10 @@ public default void extract(ICommandSender sender, File src, File dest, boolean
* The file to be compressed to.
* @param log
* Whether or not to log the progress.
* @param pipe
* Whether this output will be piped.
*/
public default void compress(ICommandSender sender, File src, File dest, boolean log) {
public default void compress(ICommandSender sender, File src, File dest, boolean log, boolean pipe) {
throw new UnsupportedOperationException();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,15 +83,16 @@ public void extract(ICommandSender sender, File src, File dest, boolean log) {
}

@Override
public void compress(ICommandSender sender, File src, File dest, boolean log) {
public void compress(ICommandSender sender, File src, File dest, boolean log, boolean pipe) {
final MessageManager mm = MessageManager.inst();
mm.startingProcess(sender, ZTask.COMPRESS, src.getName());
try (FileOutputStream fos = new FileOutputStream(dest);
XZOutputStream xzos = new XZOutputStream(fos, new LZMA2Options());) {
if (log)
mm.info("Compressing : " + src.getAbsolutePath());
xzos.write(Files.readAllBytes(src.toPath()));
mm.compressionComplete(sender, dest.getAbsolutePath());
if(!pipe)
mm.compressionComplete(sender, dest.getAbsolutePath());
} catch (IOException e) {
e.printStackTrace();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ public void extract(ICommandSender sender, File src, File dest, boolean log) {
}

@Override
public void compress(ICommandSender sender, File src, File dest, boolean log) {
public void compress(ICommandSender sender, File src, File dest, boolean log, boolean pipe) {
final MessageManager mm = MessageManager.inst();
mm.startingProcess(sender, ZTask.COMPRESS, src.getName());
try (OutputStream os = Files.newOutputStream(dest.toPath()); ZipOutputStream zs = new ZipOutputStream(os);) {
Expand All @@ -143,7 +143,8 @@ public void compress(ICommandSender sender, File src, File dest, boolean log) {
e.printStackTrace();
}
});
mm.compressionComplete(sender, dest.getAbsolutePath());
if(!pipe)
mm.compressionComplete(sender, dest.getAbsolutePath());
} catch (AccessDeniedException e) {
mm.fileAccessDenied(sender, ZTask.COMPRESS, e.getMessage());
} catch (TaskInterruptedException e) {
Expand Down

0 comments on commit 427aa74

Please sign in to comment.