/* Copyright (c) 2003, Derek Park All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of Gelaed nor Derek Park nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ import java.io.*; public class Dearchive { public static void main (String [] args) throws Exception { if (args.length < 2) { printUsage(); return; } BufferedInputStream in = new BufferedInputStream(new FileInputStream(args[0])); String name; if (args[1].equals("-list")) { while ((name = readLine(in)) != null) { System.out.println(name); String size = readLine(in); long length = Long.parseLong(size); for (int i = 0; i < length;) { i += in.skip(length - i); } } } else { File dir = new File(args[1]); dir.mkdir(); byte [] buffer = new byte[1024]; if (args[2].equals("-all")) { while ((name = readLine(in)) != null) { File f = new File(dir,name); String size = readLine(in); long length = Long.parseLong(size); BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(f)); for (long i = 0; i < length;) { int read = in.read(buffer,0,(int)(length - i >= 1024 ? 1024 : length - i)); out.write(buffer,0,read); i += read; } out.close(); } } else if (args[2].equals("-exact")) { while ((name = readLine(in)) != null) { String size = readLine(in); long length = Long.parseLong(size); boolean extracted = false; for (int j = 3; !extracted && j < args.length; j++) { if (name.equals(args[j])) { extracted = true; File f = new File(dir,name); BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(f)); for (long k = 0; k < length;) { int read = in.read(buffer,0,(int)(length - k >= 1024 ? 1024 : length - k)); out.write(buffer,0,read); k += read; } out.close(); } } if (!extracted) { for (int i = 0; i < length;) { i += in.skip(length - i); } } } } else { printUsage(); } } in.close(); } private static void printUsage() { System.out.println("usage: java Dearchive [-list] {OR} [ -all] {OR} [ -exact ]"); } private static String readLine(InputStream is) { try { char c = (char)0; StringBuffer buff = new StringBuffer(); while (c != '\n') { int i1 = is.read(); int i2 = is.read(); if (i1 == -1 || i2 == -1) return null; c = (char)(i1 << 8 | i2); buff.append(c); } return buff.substring(0,buff.length() - 1); } catch (Exception exc) { return null; } } }