Permalink
Browse files

Allow partial extract

  • Loading branch information...
1 parent 112516d commit a0dd3d34f1a42865a7fe9dff16cb223268709205 @taras-puchko taras-puchko committed May 23, 2013
@@ -16,20 +16,20 @@
package org.b1.pack.cli;
-import com.google.common.base.Preconditions;
+import com.google.common.collect.Sets;
import org.b1.pack.api.reader.PackReader;
import org.b1.pack.api.volume.VolumeFinder;
import org.b1.pack.api.volume.VolumeService;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
+import java.util.HashSet;
public class ExtractCommand implements PackCommand {
@Override
public void execute(ArgSet argSet) throws IOException {
- Preconditions.checkArgument(argSet.getFileNames().isEmpty(), "Filters not supported");
File file = new File(argSet.getPackName());
if (!file.isFile()) {
throw new FileNotFoundException("File not found: " + file);
@@ -41,8 +41,12 @@ public void execute(ArgSet argSet) throws IOException {
File parentFolder = file.getParentFile();
VolumeFinder volumeFinder = VolumeService.getInstance(argSet.getTypeFormat())
.createVolumeFinder(new FsVolumeFinderProvider(parentFolder, file.getName()));
- PackReader.getInstance(argSet.getTypeFormat())
- .read(new FsReaderProvider(new FsFolderBuilder(outputFolder, null), parentFolder, volumeFinder, argSet.getPassword()));
+ HashSet<String> fileNameSet = argSet.getFileNames().isEmpty() ? null : Sets.newHashSet(argSet.getFileNames());
+ FsFolderBuilder folderBuilder = new FsFolderBuilder(outputFolder, null, "", fileNameSet);
+ PackReader.getInstance(argSet.getTypeFormat()).read(new FsReaderProvider(folderBuilder, parentFolder, volumeFinder, argSet.getPassword()));
+ if (fileNameSet != null && !fileNameSet.isEmpty()) {
+ throw new IllegalStateException("Cannot extract: " + fileNameSet);
+ }
System.out.println();
System.out.println("Done");
}
@@ -22,36 +22,67 @@
import org.b1.pack.api.common.PackEntry;
import java.io.File;
+import java.util.Set;
public class FsFolderBuilder implements FolderBuilder {
private final File targetFolder;
private final Long lastModifiedTime;
+ private final String prefixInArchive;
+ private final Set<String> fileNameSet;
- public FsFolderBuilder(File targetFolder, Long lastModifiedTime) {
+ public FsFolderBuilder(File targetFolder, Long lastModifiedTime, String prefixInArchive, Set<String> fileNameSet) {
this.targetFolder = targetFolder;
this.lastModifiedTime = lastModifiedTime;
+ this.prefixInArchive = prefixInArchive;
+ this.fileNameSet = fileNameSet;
}
@Override
public FileBuilder addFile(PackEntry entry, Long size) {
- File nativeFile = getNativeFile(entry.getName());
- return new FsFileBuilder(nativeFile, entry.getLastModifiedTime());
+ if (fileNameSet == null || fileNameSet.remove(getPath(entry))) {
+ File nativeFile = getNativeFile(entry.getName());
+ return new FsFileBuilder(nativeFile, entry.getLastModifiedTime());
+ }
+ return null;
}
@Override
public FolderBuilder addFolder(PackEntry entry) {
- File nativeFile = getNativeFile(entry.getName());
- System.out.println("Creating " + nativeFile);
- Preconditions.checkState(nativeFile.mkdir(), "Cannot create folder: %s", nativeFile);
- return new FsFolderBuilder(nativeFile, entry.getLastModifiedTime());
+ String path = getPath(entry);
+ String prefix = path + "/";
+ if (fileNameSet == null || fileNameSet.remove(path)) {
+ return getFolderBuilder(entry, prefix, null);
+ }
+ if (namesWithPrefixPresent(prefix)) {
+ return getFolderBuilder(entry, prefix, fileNameSet);
+ }
+ return null;
}
@Override
public void save() {
FileTools.setLastModified(targetFolder, lastModifiedTime);
}
+ private String getPath(PackEntry entry) {
+ return prefixInArchive + entry.getName();
+ }
+
+ private FolderBuilder getFolderBuilder(PackEntry entry, String prefix, Set<String> fileNameSet) {
+ File nativeFile = getNativeFile(entry.getName());
+ System.out.println("Creating " + nativeFile);
+ Preconditions.checkState(nativeFile.mkdir(), "Cannot create folder: %s", nativeFile);
+ return new FsFolderBuilder(nativeFile, entry.getLastModifiedTime(), prefix, fileNameSet);
+ }
+
+ private boolean namesWithPrefixPresent(String prefix) {
+ for (String fileName : fileNameSet) {
+ if (fileName.startsWith(prefix)) return true;
+ }
+ return false;
+ }
+
private File getNativeFile(String name) {
return targetFolder == null ? new File(name) : new File(targetFolder, name);
}

0 comments on commit a0dd3d3

Please sign in to comment.