Support for using 7z and unrar tools
This commit is contained in:
parent
21d3e8daec
commit
d856933588
|
@ -11,10 +11,8 @@ import java.io.FileFilter;
|
||||||
import java.io.FileNotFoundException;
|
import java.io.FileNotFoundException;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.lang.ProcessBuilder.Redirect;
|
import java.lang.ProcessBuilder.Redirect;
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
import java.util.stream.Stream;
|
|
||||||
|
|
||||||
import net.filebot.util.ByteBufferOutputStream;
|
import net.filebot.util.ByteBufferOutputStream;
|
||||||
import net.filebot.util.FileUtilities.ExtensionFileFilter;
|
import net.filebot.util.FileUtilities.ExtensionFileFilter;
|
||||||
|
@ -31,8 +29,8 @@ public class ShellExecutables implements ArchiveExtractor {
|
||||||
throw new FileNotFoundException(archive.getPath());
|
throw new FileNotFoundException(archive.getPath());
|
||||||
}
|
}
|
||||||
|
|
||||||
this.command = getCommand(archive);
|
this.archive = archive.getCanonicalFile();
|
||||||
this.archive = archive;
|
this.command = getCommand(this.archive);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -42,18 +40,22 @@ public class ShellExecutables implements ArchiveExtractor {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void extract(File outputFolder) throws IOException {
|
public void extract(File outputFolder) throws IOException {
|
||||||
command.extract(archive, outputFolder);
|
command.extract(archive, outputFolder.getCanonicalFile());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void extract(File outputFolder, FileFilter filter) throws IOException {
|
public void extract(File outputFolder, FileFilter filter) throws IOException {
|
||||||
command.extract(archive, outputFolder, filter);
|
command.extract(archive, outputFolder.getCanonicalFile(), filter);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected static Command getCommand(File archive) {
|
protected static Command getCommand(File archive) {
|
||||||
return RAR_FILES.accept(archive) ? Command.unrar : Command.p7zip;
|
return RAR_FILES.accept(archive) ? Command.unrar : Command.p7zip;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected static CharSequence execute(String[]... command) throws IOException {
|
||||||
|
return execute(stream(command).flatMap(a -> stream(a)).toArray(String[]::new));
|
||||||
|
}
|
||||||
|
|
||||||
protected static CharSequence execute(String... command) throws IOException {
|
protected static CharSequence execute(String... command) throws IOException {
|
||||||
Process process = new ProcessBuilder(command).redirectError(Redirect.INHERIT).start();
|
Process process = new ProcessBuilder(command).redirectError(Redirect.INHERIT).start();
|
||||||
|
|
||||||
|
@ -88,64 +90,35 @@ public class ShellExecutables implements ArchiveExtractor {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<FileInfo> listFiles(File archive) throws IOException {
|
public List<FileInfo> listFiles(File archive) throws IOException {
|
||||||
CharSequence output = execute(getCommand(), "l", "-slt", "-y", archive.getPath());
|
CharSequence output = execute(getCommand(), "l", "-y", archive.getPath());
|
||||||
|
|
||||||
List<FileInfo> paths = new ArrayList<FileInfo>();
|
return NEWLINE.splitAsStream(output).map(String::trim).map(l -> SPACE.split(l, 6)).filter(r -> {
|
||||||
|
return r.length == 6 && !r[5].isEmpty() && NON_DIGIT.matcher(r[2]).matches() && DIGIT.matcher(r[3]).matches();
|
||||||
String path = null;
|
}).map(r -> {
|
||||||
long size = -1;
|
return new SimpleFileInfo(r[5], Long.parseLong(r[3]));
|
||||||
|
}).collect(toList());
|
||||||
for (String line : NEWLINE.split(output)) {
|
|
||||||
int split = line.indexOf(" = ");
|
|
||||||
|
|
||||||
// ignore empty lines
|
|
||||||
if (split < 0) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
String key = line.substring(0, split);
|
|
||||||
String value = line.substring(split + 3, line.length());
|
|
||||||
|
|
||||||
// ignore empty lines
|
|
||||||
if (key.isEmpty() || value.isEmpty()) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if ("Path".equals(key)) {
|
|
||||||
path = value;
|
|
||||||
} else if ("Size".equals(key)) {
|
|
||||||
size = Long.parseLong(value);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (path != null && size >= 0) {
|
|
||||||
paths.add(new SimpleFileInfo(path, size));
|
|
||||||
|
|
||||||
path = null;
|
|
||||||
size = -1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return paths;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void extract(File archive, File outputFolder) throws IOException {
|
public void extract(File archive, File outputFolder) throws IOException {
|
||||||
execute(getCommand(), "x", "-y", "-aos", archive.getPath(), "-o" + outputFolder.getCanonicalPath());
|
String[] command = { getCommand(), "x", "-y", "-aos", archive.getPath(), "-o" + outputFolder.getPath() };
|
||||||
|
|
||||||
|
execute(command);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void extract(File archive, File outputFolder, FileFilter filter) throws IOException {
|
public void extract(File archive, File outputFolder, FileFilter filter) throws IOException {
|
||||||
Stream<String> command = Stream.of(getCommand(), "x", "-y", "-aos", archive.getPath(), "-o" + outputFolder.getCanonicalPath());
|
String[] command = { getCommand(), "x", "-y", "-aos", archive.getPath(), "-o" + outputFolder.getPath() };
|
||||||
Stream<String> selection = listFiles(archive).stream().filter(f -> filter.accept(f.toFile())).map(FileInfo::getPath);
|
String[] selection = listFiles(archive).stream().filter(f -> filter.accept(f.toFile())).map(FileInfo::getPath).toArray(String[]::new);
|
||||||
|
|
||||||
execute(Stream.concat(command, selection).toArray(String[]::new));
|
execute(command, selection);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String version() throws IOException {
|
public String version() throws IOException {
|
||||||
CharSequence output = execute(getCommand());
|
CharSequence output = execute(getCommand());
|
||||||
|
|
||||||
return NEWLINE.splitAsStream(output).map(String::trim).filter(s -> s.startsWith("p7zip")).findFirst().get();
|
return NEWLINE.splitAsStream(output).map(String::trim).filter(s -> s.startsWith("p7zip")).findFirst().orElse(null);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
|
@ -161,7 +134,7 @@ public class ShellExecutables implements ArchiveExtractor {
|
||||||
CharSequence output = execute(getCommand(), "l", "-y", archive.getPath());
|
CharSequence output = execute(getCommand(), "l", "-y", archive.getPath());
|
||||||
|
|
||||||
return NEWLINE.splitAsStream(output).map(String::trim).map(l -> SPACE.split(l, 5)).filter(r -> {
|
return NEWLINE.splitAsStream(output).map(String::trim).map(l -> SPACE.split(l, 5)).filter(r -> {
|
||||||
return r.length == 5 && r[4].length() > 0 && DIGIT.matcher(r[1]).matches();
|
return r.length == 5 && r[4].length() > 0 && NON_DIGIT.matcher(r[0]).matches() && DIGIT.matcher(r[1]).matches();
|
||||||
}).map(r -> {
|
}).map(r -> {
|
||||||
return new SimpleFileInfo(r[4], Long.parseLong(r[1]));
|
return new SimpleFileInfo(r[4], Long.parseLong(r[1]));
|
||||||
}).collect(toList());
|
}).collect(toList());
|
||||||
|
@ -169,23 +142,25 @@ public class ShellExecutables implements ArchiveExtractor {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void extract(File archive, File outputFolder) throws IOException {
|
public void extract(File archive, File outputFolder) throws IOException {
|
||||||
execute(getCommand(), "x", "-y", archive.getPath(), outputFolder.getCanonicalPath());
|
String[] command = { getCommand(), "x", "-y", archive.getPath(), outputFolder.getPath() };
|
||||||
|
|
||||||
|
execute(command);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void extract(File archive, File outputFolder, FileFilter filter) throws IOException {
|
public void extract(File archive, File outputFolder, FileFilter filter) throws IOException {
|
||||||
Stream<String> command = Stream.of(getCommand(), "x", "-y", archive.getPath());
|
String[] command = { getCommand(), "x", "-y", archive.getPath() };
|
||||||
Stream<String> selection = listFiles(archive).stream().filter(f -> filter.accept(f.toFile())).map(FileInfo::getPath);
|
String[] selection = listFiles(archive).stream().filter(f -> filter.accept(f.toFile())).map(FileInfo::getPath).toArray(String[]::new);
|
||||||
Stream<String> output = Stream.of(outputFolder.getCanonicalPath());
|
String[] output = { outputFolder.getPath() };
|
||||||
|
|
||||||
execute(Stream.concat(command, Stream.concat(selection, output)).toArray(String[]::new));
|
execute(command, selection, output);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String version() throws IOException {
|
public String version() throws IOException {
|
||||||
CharSequence output = execute(getCommand());
|
CharSequence output = execute(getCommand());
|
||||||
|
|
||||||
return Pattern.compile("\\n|\\s\\s+").splitAsStream(output).map(String::trim).filter(s -> s.length() > 0).findFirst().get();
|
return Pattern.compile("\\n+|\\s\\s+").splitAsStream(output).map(String::trim).filter(s -> !s.isEmpty()).findFirst().orElse(null);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue