diff --git a/src/Debugging.java b/src/Debugging.java index 27ad0e2..e14f4b0 100644 --- a/src/Debugging.java +++ b/src/Debugging.java @@ -1,7 +1,7 @@ public class Debugging { //debugging flags - public static final boolean LISTER = true; - public static final boolean FLAG_INTERNAL = true; + public static final boolean LISTER = false; + public static final boolean FLAG_INTERNAL = false; } diff --git a/src/Lister.java b/src/Lister.java deleted file mode 100644 index 666b92c..0000000 --- a/src/Lister.java +++ /dev/null @@ -1,48 +0,0 @@ -import java.io.BufferedInputStream; -import java.io.File; -import java.io.InputStream; -import java.nio.file.Files; -import java.util.ArrayList; -import java.util.List; - -import org.apache.commons.compress.archivers.*; - -public final class Lister { - private static final ArchiveStreamFactory factory = new ArchiveStreamFactory(); - - @SuppressWarnings("null") - public static List getEntriesList(final String fn) throws Exception { - if (Debugging.LISTER) { - System.out.println("Analyzing " + fn + ". . ."); - } - - final File f = new File(fn); - - if (!f.isFile()) { - //as we're conscripting this for our own purposes, might as well throw an exception - //here, I suppose - System.err.println(f.getName() + " doesn't exist or is a directory"); - } - - //okay, so I do understand this syntax now, but I really must say, it reduces the shit - //out of code readability, as far as I'm concerned; it's not originally my code, at - //least, so I'm not going to worry about its aesthetics. It's all on you, apache... - List directory = new ArrayList(); - try (final InputStream fis = new BufferedInputStream(Files.newInputStream(f.toPath())); - final ArchiveInputStream ais = factory.createArchiveInputStream(fis)) { - if (RAS.VERBOSE) { - System.out.println("Created " + ais.toString()); - } - - ArchiveEntry ae; - while ((ae = ais.getNextEntry()) != null) { - if (RAS.VERBOSE) { - System.out.println(ae.getName()); - } - directory.add(ae.getName()); - } - } - - return directory; - } -} \ No newline at end of file diff --git a/src/MyArchive.java b/src/MyArchive.java index e369d3f..9990007 100644 --- a/src/MyArchive.java +++ b/src/MyArchive.java @@ -1,20 +1,20 @@ import java.io.BufferedInputStream; import java.io.File; -import java.io.FileInputStream; +import java.io.FileOutputStream; import java.io.InputStream; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.attribute.FileAttribute; import java.nio.file.attribute.PosixFilePermission; import java.nio.file.attribute.PosixFilePermissions; +import java.util.ArrayList; import java.util.HashMap; import java.util.Set; import org.apache.commons.compress.archivers.ArchiveEntry; import org.apache.commons.compress.archivers.ArchiveInputStream; import org.apache.commons.compress.archivers.ArchiveStreamFactory; -import org.apache.commons.compress.compressors.CompressorInputStream; -import org.apache.commons.compress.compressors.CompressorStreamFactory; +import org.apache.commons.compress.utils.IOUtils; /** * @@ -26,6 +26,7 @@ public class MyArchive { private Path unrollPath; private Boolean containsArchives; private File archiveSource; + private ArrayList archiveContents; Set perms = PosixFilePermissions.fromString("rwx------"); @@ -33,25 +34,14 @@ public class MyArchive { PosixFilePermissions.asFileAttribute(perms); public MyArchive(String fn) throws Exception { - Boolean stupidFlag = false; - + this.archiveContents = null; + this.unrollPath = null; + this.arcFileName = fn; this.archiveSource = new File(fn); if (!archiveSource.isFile()) { throw new Exception("Invalid archive source: " + fn); } - - this.arcFileName = fn; - - /*for (String extension : Util.flagExtensions) { - if (this.arcFileName.toLowerCase().endsWith(extension)) { - stupidFlag = true; - break; - } - } - if (!stupidFlag) { - throw new Exception("Invalid archive source file extension"); - }*/ } //getters/setters @@ -72,24 +62,30 @@ public File getArchiveSource() { } //methods + private ArchiveInputStream getArchiveInputStream() throws Exception { + InputStream fis = new BufferedInputStream( + Files.newInputStream(this.archiveSource.toPath())); + + return new ArchiveStreamFactory().createArchiveInputStream(fis); + } + + private void setUnrollPath() throws Exception { + if (unrollPath == null && !Util.runningOnDoze()) { + unrollPath = Files.createTempDirectory("RAS_", attr); + } else if (unrollPath == null) { + unrollPath = Files.createTempDirectory("RAS_"); + } + } + public HashMap getEntryHash() throws Exception { HashMap entryData = new HashMap(); ArchiveEntry entry = null; - /* FileInputStream fis = new FileInputStream(this.archiveSource); - ArchiveInputStream ais = (ArchiveInputStream) fis; - CompressorInputStream cis = - new CompressorStreamFactory().createCompressorInputStream(fis); - try (final InputStream fis = new BufferedInputStream(Files.newInputStream(f.toPath())); - final ArchiveInputStream ais = factory.createArchiveInputStream(fis)) { */ - InputStream fis = new BufferedInputStream( - Files.newInputStream(this.archiveSource.toPath())); - ArchiveInputStream ais = - new ArchiveStreamFactory().createArchiveInputStream(fis); + ArchiveInputStream ais = getArchiveInputStream(); try { - unrollPath = Files.createTempDirectory("RAS"/*, attr*/); + setUnrollPath(); } catch (Exception ex) { - fis.close(); /*cis.close();*/ ais.close(); + ais.close(); throw new Exception("Issue creating temp dir: " + ex.toString()); } @@ -103,15 +99,44 @@ public HashMap getEntryHash() throws Exception { break; } } + archiveContents.add(entry.getName()); entryData.put(entry.getName(), hit); hit = false; } } catch (Exception ex) { - fis.close(); /*cis.close();*/ ais.close(); + ais.close(); throw new Exception("Issue getting entry names: " + ex.toString()); } - fis.close(); ais.close(); + ais.close(); return entryData; } + + public void unroll() throws Exception { + ArchiveEntry entry; + File currentFile, parentFile; + + ArchiveInputStream ais = getArchiveInputStream(); + + setUnrollPath(); + + if (RAS.VERBOSE) { + System.out.println("Attempting to expand " + arcFileName + + " to " + unrollPath); + } + + while ((entry = ais.getNextEntry()) != null) { + if (entry.isDirectory()) { + continue; + } + + currentFile = new File(unrollPath.toFile(), entry.getName()); + parentFile = currentFile.getParentFile(); + if (!parentFile.exists()) { + parentFile.mkdirs(); + } + + IOUtils.copy(ais, new FileOutputStream(currentFile)); + } + } } diff --git a/src/RAS.java b/src/RAS.java index cdd4e43..b8b675e 100644 --- a/src/RAS.java +++ b/src/RAS.java @@ -1,10 +1,4 @@ -import java.util.ArrayList; import java.util.HashMap; -import java.util.Iterator; -import java.util.List; -import java.util.Map.Entry; -import java.util.Set; - /** * RAS - Recursive Archive Scanner @@ -27,11 +21,7 @@ public class RAS { public static final boolean VERBOSE = true; public static final boolean UNROLL_FIRST = false; - public static final boolean USING_LISTER = false; //mine or apache's? - - //public static final String tmpDir = new String("/tmp"); - //public static final String tmpDir = System.getProperty("java.io.tmpdir") - // + "/RAS"; + public static final boolean USING_LISTER = false; /** * @param args @@ -41,40 +31,35 @@ public static void main(String[] args) { //dump usage usage(); return; - } else if (args.length == 1 && !args[0].equals("-v")) { + } else if (args[0].equals("-v")) { + System.err.println("Sorry, verbose is set by default at this " + + "point in development.\n"); + usage(); + return; + } else if (args.length == 1 /*&& !args[0].equals("-v")*/) { //list contents - if (USING_LISTER) { - List directory = new ArrayList(); - - try { - directory = Lister.getEntriesList(args[0]); - } catch (Exception e) { - e.printStackTrace(); - } - - System.out.println(directory.toString()); - } else { - HashMap directory = - new HashMap(); - - try { - MyArchive archive = new MyArchive(args[0]); - directory = archive.getEntryHash(); - } catch (Exception ex) { - System.err.println("MyArchive Error: " + - ex.getMessage()); - } - - for (String entry : directory.keySet()) { - if (directory.get(entry)) { - System.out.print(" * "); - } else { - System.out.print(" "); - } - System.out.println(entry); - } + HashMap directory = + new HashMap(); + + try { + MyArchive archive = new MyArchive(args[0]); + directory = archive.getEntryHash(); + } catch (Exception ex) { + System.err.println("MyArchive Error: " + + ex.getMessage()); } - } else { + + displayEntryData(directory); + } else if (args.length == 2 && args[0].equals("-x")) { + //expand contents + try { + MyArchive archive = new MyArchive(args[1]); + archive.unroll(); + } catch (Exception ex) { + System.err.println("MyArchive Error: " + + ex.getMessage()); + } + } else { //we're not there yet System.out.println("Not supported yet . . ."); usage(); @@ -87,18 +72,19 @@ private static void usage() { } private static void displayEntryData(HashMap eData) { - Set archiveEntries = eData.keySet(); - Iterator entry = eData.keySet().iterator(); - if (VERBOSE) { System.out.println( "Entry Text (starred if flagged as recursive archive)\n" + "----------------------------------------------------"); } - while (entry.hasNext()) { - HashMap.Entry ouah = - (Entry) entry.next(); + for (String entry : eData.keySet()) { + if (eData.get(entry)) { + System.out.print(" * "); + } else { + System.out.print(" "); + } + System.out.println(entry); } } diff --git a/src/Util.java b/src/Util.java index d3d0e81..382c836 100644 --- a/src/Util.java +++ b/src/Util.java @@ -1,40 +1,11 @@ -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; - public class Util { public static final String[] flagExtensions = { ".tar", ".tar.gz", ".tgz", ".tar.bz2", - ".tar.xz", ".zip", ".arj", ".tar.Z" }; - /** - * Flag archives in the directory listing (by extension, not signature) in order to mark - * them for later unrolling or other processing - */ - /*public static HashMap flagArchives(HashMap directory) { - List internalArchives = new ArrayList(); - Boolean hit = false; - - for (String entry : directory) { - for (String ext : flagExtensions) { - if (entry.toLowerCase().endsWith(ext)) { - hit = true; - break; - } - } - - if (hit) { - directory.put(entry, true); - } - } - - return internalArchives; - }*/ + ".tar.xz", ".zip", ".arj", ".tar.Z" }; + public static final String[] archiveExtensions = { ".tar", ".zip", ".arj" }; + public static final String[] compressExtensions = { ".gz", ".tgz", ".bz2", ".xz", ".zip", + ".arj", ".Z" }; public static Boolean runningOnDoze() { return System.getProperty("os.name").startsWith("Windows"); } - - /** - * Unroll archives (including recursive archival) - */ - }