From 4358c86618d1f16e8795fcb92147842ea131f4e1 Mon Sep 17 00:00:00 2001 From: Date: Sat, 20 Dec 2008 03:31:54 +0000 Subject: [PATCH] Add the ability to extract everything from an image using its preferred export method. --- src/com/webcodepro/applecommander/ui/ac.java | 38 ++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/src/com/webcodepro/applecommander/ui/ac.java b/src/com/webcodepro/applecommander/ui/ac.java index 3ff6c2f..8f6ac52 100644 --- a/src/com/webcodepro/applecommander/ui/ac.java +++ b/src/com/webcodepro/applecommander/ui/ac.java @@ -22,7 +22,10 @@ package com.webcodepro.applecommander.ui; import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.FileOutputStream; import java.io.IOException; +import java.io.OutputStream; import java.util.Iterator; import java.util.List; @@ -56,6 +59,7 @@ import com.webcodepro.applecommander.util.TextBundle; * -l <imagename> [<imagename>] list directory of image(s). * -ll <imagename> [<imagename>] list detailed directory of image(s). * -e <imagename> <filename> export file from image to stdout. + * -x <imagename> [<directory>] export all files from image to directory. * -g <imagename> <filename> get raw file from image to stdout. * -p <imagename> <filename> <type> [[$|0x]<addr>] put stdin * in filename on image, using file type and address [0x2000]. @@ -91,6 +95,8 @@ public class ac { showDirectory(args, FormattedDisk.FILE_DISPLAY_DETAIL); } else if ("-e".equalsIgnoreCase(args[0])) { //$NON-NLS-1$ getFile(args[1], args[2], true); + } else if ("-x".equalsIgnoreCase(args[0])) { //$NON-NLS-1$ + getFiles(args[1], (args.length > 2 ? args[2] : "")); } else if ("-g".equalsIgnoreCase(args[0])) { //$NON-NLS-1$ getFile(args[1], args[2], false); } else if ("-p".equalsIgnoreCase(args[0])) { //$NON-NLS-1$ @@ -218,6 +224,38 @@ public class ac { } } + /** + * Extract all files in the image according to their respective filetype. + */ + static void getFiles(String imageName, String directory) throws IOException { + Disk disk = new Disk(imageName); + if (directory.length() > 0) { + // Add a final directory separator if the user didn't supply one + if (!directory.endsWith(System.getProperty("file.separator"))) + directory = directory + System.getProperty("file.separator"); + } + FormattedDisk[] formattedDisks = disk.getFormattedDisks(); + for (int i = 0; i < formattedDisks.length; i++) { + FormattedDisk formattedDisk = formattedDisks[i]; + List files = formattedDisk.getFiles(); + Iterator it = files.iterator(); + while (it.hasNext()) { + FileEntry entry = (FileEntry) it.next(); + if ((entry != null) && (!entry.isDeleted())) { + FileFilter ff = entry.getSuggestedFilter(); + if (ff instanceof BinaryFileFilter) + ff = new HexDumpFileFilter(); + byte[] buf = ff.filter(entry); + String filename = ff.getSuggestedFileName(entry); + File file = new File(directory + filename); + OutputStream output = new FileOutputStream(file); + output.write(buf, 0, buf.length); + output.close(); + } + } + } + } + /** * Recursive routine to locate a specific file by filename; In the instance * of a system with directories (e.g. ProDOS), this really returns the first