2014-04-19 06:30:29 +00:00
|
|
|
package net.filebot.archive;
|
2012-02-26 12:58:16 +00:00
|
|
|
|
2017-09-22 13:54:08 +07:00
|
|
|
import static java.util.stream.Collectors.*;
|
|
|
|
|
2012-02-26 12:58:16 +00:00
|
|
|
import java.io.File;
|
2017-09-22 13:54:08 +07:00
|
|
|
import java.io.FileFilter;
|
2012-02-26 12:58:16 +00:00
|
|
|
import java.io.FileOutputStream;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.OutputStream;
|
2017-09-22 13:54:08 +07:00
|
|
|
import java.util.Collection;
|
|
|
|
import java.util.Set;
|
|
|
|
|
|
|
|
import net.filebot.vfs.FileInfo;
|
2012-02-26 12:58:16 +00:00
|
|
|
|
|
|
|
public class FileMapper implements ExtractOutProvider {
|
2015-03-25 22:38:15 +00:00
|
|
|
|
2012-02-26 12:58:16 +00:00
|
|
|
private File outputDir;
|
|
|
|
private boolean flatten;
|
2015-03-25 22:38:15 +00:00
|
|
|
|
2015-03-26 08:40:57 +00:00
|
|
|
public FileMapper(File outputDir) {
|
|
|
|
this(outputDir, false);
|
|
|
|
};
|
|
|
|
|
2012-02-26 12:58:16 +00:00
|
|
|
public FileMapper(File outputDir, boolean flatten) {
|
|
|
|
this.outputDir = outputDir;
|
|
|
|
this.flatten = flatten;
|
|
|
|
};
|
2015-03-25 22:38:15 +00:00
|
|
|
|
|
|
|
public File getOutputDir() {
|
|
|
|
return outputDir;
|
|
|
|
}
|
|
|
|
|
2012-02-26 12:58:16 +00:00
|
|
|
public File getOutputFile(File entry) {
|
|
|
|
return new File(outputDir, flatten ? entry.getName() : entry.getPath());
|
|
|
|
}
|
2015-03-25 22:38:15 +00:00
|
|
|
|
2012-02-26 12:58:16 +00:00
|
|
|
@Override
|
|
|
|
public OutputStream getStream(File entry) throws IOException {
|
|
|
|
File outputFile = getOutputFile(entry);
|
|
|
|
File outputFolder = outputFile.getParentFile();
|
2015-03-25 22:38:15 +00:00
|
|
|
|
2012-02-26 12:58:16 +00:00
|
|
|
// create parent folder if necessary
|
|
|
|
if (!outputFolder.isDirectory() && !outputFolder.mkdirs()) {
|
|
|
|
throw new IOException("Failed to create folder: " + outputFolder);
|
|
|
|
}
|
2015-03-25 22:38:15 +00:00
|
|
|
|
2012-02-26 12:58:16 +00:00
|
|
|
return new FileOutputStream(outputFile);
|
|
|
|
}
|
2017-09-22 13:54:08 +07:00
|
|
|
|
|
|
|
public FileFilter newPathFilter(Collection<FileInfo> selection) {
|
|
|
|
return newPathFilter(selection.stream().map(FileInfo::getPath).collect(toSet()));
|
|
|
|
}
|
|
|
|
|
|
|
|
public FileFilter newPathFilter(Set<String> selection) {
|
|
|
|
return f -> selection.contains(getOutputFile(f).getPath());
|
|
|
|
}
|
|
|
|
|
2012-02-26 12:58:16 +00:00
|
|
|
}
|