Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- public class Extractor {
- public static void main(String[] args) {
- if (args.length != 2) {
- System.out.println("Specify a file and output dir!");
- } else {
- File in = new File(args[0]);
- File out = new File(args[1]);
- extract(in, out);
- }
- }
- private static void extract(File inFile, File out) {
- try {
- DataInputStream in = new DataInputStream(new FileInputStream(inFile));
- byte[] filenameBytes = new byte[60];
- while (in.available() > 0) {
- int nameLen = in.read(filenameBytes);
- char[] chars = new char[nameLen];
- for (int i = 0; i < nameLen; i++) {
- if (filenameBytes[i] == 0) {
- break;
- }
- chars[i] = (char)filenameBytes[i];
- }
- String name = new String(chars).trim();
- int length = in.readInt();
- extractFile(out, name, length, in);
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- private static void extractFile(File outDir, String name, int length, DataInputStream in) throws IOException {
- File outFile = new File(outDir, name);
- makeDirs(outFile);
- System.out.println("Extracting " + name + " to " + outFile.getPath());
- OutputStream out = new FileOutputStream(outFile);
- byte[] buff = new byte[length];
- int read = in.read(buff);
- if (read != length) {
- System.err.println("Number of read bytes did not match header!");
- }
- out.write(buff);
- out.close();
- }
- private static void makeDirs(File file) {
- String path = file.getPath();
- int idx = path.lastIndexOf('/');
- if (idx != -1) {
- File dir = new File(path.substring(0, idx + 1));
- dir.mkdirs();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement