Refactor net.filebot.Archive.extractor

This commit is contained in:
Reinhard Pointner 2016-10-31 16:47:01 +08:00
parent c7f5fe9364
commit 42ae55f9da
4 changed files with 32 additions and 35 deletions

View File

@ -11,7 +11,6 @@ import java.util.prefs.BackingStoreException;
import java.util.prefs.Preferences;
import net.filebot.UserFiles.FileChooser;
import net.filebot.archive.Archive.Extractor;
import net.filebot.cli.ArgumentBean;
import net.filebot.util.PreferencesList;
import net.filebot.util.PreferencesMap;
@ -126,10 +125,6 @@ public final class Settings {
return FileChooser.valueOf(System.getProperty("net.filebot.UserFiles.fileChooser", "Swing"));
}
public static Extractor getPreferredArchiveExtractor() {
return Extractor.valueOf(System.getProperty("net.filebot.Archive.extractor", "SevenZipNativeBindings"));
}
public static int getPreferredThreadPoolSize() {
try {
String threadPool = System.getProperty("threadPool");

View File

@ -1,5 +1,7 @@
package net.filebot.archive;
import static java.util.Arrays.*;
import static net.filebot.MediaTypes.*;
import static net.filebot.util.StringUtilities.*;
import java.io.Closeable;
@ -7,19 +9,26 @@ import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Stream;
import org.apache.commons.vfs2.FileSystemException;
import org.apache.commons.vfs2.VFS;
import net.filebot.MediaTypes;
import net.filebot.Settings;
import net.filebot.util.FileUtilities.ExtensionFileFilter;
import net.filebot.util.SystemProperty;
import net.filebot.vfs.FileInfo;
import net.sf.sevenzipjbinding.ArchiveFormat;
public class Archive implements Closeable {
public static Extractor getExtractor() {
return SystemProperty.of("net.filebot.Archive.extractor", Extractor::valueOf, Extractor.SevenZipNativeBindings).get();
}
public static enum Extractor {
SevenZipNativeBindings, SevenZipExecutable, ApacheVFS;
public ArchiveExtractor newInstance(File archive) throws Exception {
@ -28,15 +37,28 @@ public class Archive implements Closeable {
return new SevenZipNativeBindings(archive);
case SevenZipExecutable:
return new SevenZipExecutable(archive);
case ApacheVFS:
default:
return new ApacheVFS(archive);
}
return null;
}
public String[] getSupportedTypes() {
switch (this) {
case SevenZipNativeBindings:
case SevenZipExecutable:
return stream(ArchiveFormat.values()).map(ArchiveFormat::getMethodName).toArray(String[]::new);
default:
try {
return VFS.getManager().getSchemes();
} catch (FileSystemException e) {
throw new IllegalStateException(e);
}
}
}
}
public static Archive open(File archive) throws Exception {
return new Archive(Settings.getPreferredArchiveExtractor().newInstance(archive));
return new Archive(getExtractor().newInstance(archive));
}
private final ArchiveExtractor extractor;
@ -64,16 +86,8 @@ public class Archive implements Closeable {
}
}
public static Set<String> getArchiveTypes() {
Set<String> extensions = new TreeSet<String>(String.CASE_INSENSITIVE_ORDER);
// application data
extensions.addAll(MediaTypes.getDefault().getExtensionList("archive"));
// formats provided by the library
extensions.addAll(SevenZipNativeBindings.getArchiveTypes());
return extensions;
public static String[] getArchiveTypes() {
return Stream.of(ARCHIVE_FILES.extensions(), Extractor.SevenZipNativeBindings.getSupportedTypes()).flatMap(Stream::of).distinct().toArray(String[]::new);
}
private static final Pattern multiPartIndex = Pattern.compile("[.][0-9]{3}$");

View File

@ -12,7 +12,6 @@ import java.util.Map;
import net.filebot.vfs.FileInfo;
import net.filebot.vfs.SimpleFileInfo;
import net.sf.sevenzipjbinding.ArchiveFormat;
import net.sf.sevenzipjbinding.IInArchive;
import net.sf.sevenzipjbinding.PropID;
import net.sf.sevenzipjbinding.SevenZipException;
@ -119,15 +118,4 @@ public class SevenZipNativeBindings implements ArchiveExtractor, Closeable {
}
}
public static List<String> getArchiveTypes() {
List<String> extensions = new ArrayList<String>();
// formats provided by the library
for (ArchiveFormat it : ArchiveFormat.values()) {
extensions.add(it.getMethodName());
}
return extensions;
}
}

View File

@ -115,7 +115,7 @@ public class ScriptShellMethods {
try {
return MediaDetection.isVideoDiskFile(self);
} catch (Exception e) {
debug.log(Level.WARNING, format("Failed to read disk image: %s: %s", self, e));
debug.log(Level.WARNING, "Failed to read disk image: " + e);
}
}