Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

162
LINES

< > BotCompany Repo | #1008995 // Extract RAR [WORKS]

JavaX source code [tags: use-pretranspiled] - run with: x30.jar

Uses 201K of libraries. Click here for Pure Java version (200L/2K/5K).

!7

lib 1008994 // junrar
lib 1003619 // apache logging

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.github.junrar.Archive;
import com.github.junrar.exception.RarException;
import com.github.junrar.rarfile.FileHeader;

/**
 * extract an archive to the given location
 * 
 * @author edmund wagner
 * 
 */

    private static Log logger = LogFactory.getLog(main.class
	    .getName());

    public static void extractArchive(String archive, String destination) {
	if (archive == null || destination == null) {
	    throw new RuntimeException("archive and destination must me set");
	}
	File arch = new File(archive);
	if (!arch.exists()) {
	    throw new RuntimeException("the archive does not exit: " + archive);
	}
	File dest = new File(destination);
	if (!dest.exists() || !dest.isDirectory()) {
	    throw new RuntimeException(
		    "the destination must exist and point to a directory: "
			    + destination);
	}
	extractArchive(arch, dest);
    }

    p {
	if (args.length == 2) {
	    extractArchive(args[0], args[1]);
	} else {
	    System.out
		    .println("usage: java -jar extractArchive.jar <thearchive> <the destination directory>");
	}
    }

    public static void extractArchive(File archive, File destination) {
	Archive arch = null;
	try {
	    arch = new Archive(archive);
	} catch (RarException e) {
	    logger.error(e);
	} catch (IOException e1) {
	    logger.error(e1);
	}
	if (arch != null) {
	    if (arch.isEncrypted()) {
		logger.warn("archive is encrypted cannot extreact");
		return;
	    }
	    FileHeader fh = null;
	    while (true) {
		fh = arch.nextFileHeader();
		if (fh == null) {
		    break;
		}
		if (fh.isEncrypted()) {
		    logger.warn("file is encrypted cannot extract: "
			    + fh.getFileNameString());
		    continue;
		}
		logger.info("extracting: " + fh.getFileNameString());
		try {
		    if (fh.isDirectory()) {
			createDirectory(fh, destination);
		    } else {
			File f = createFile(fh, destination);
			OutputStream stream = new FileOutputStream(f);
			arch.extractFile(fh, stream);
			stream.close();
		    }
		} catch (IOException e) {
		    logger.error("error extracting the file", e);
		} catch (RarException e) {
		    logger.error("error extraction the file", e);
		}
	    }
	}
    }

    private static File createFile(FileHeader fh, File destination) {
	File f = null;
	String name = null;
	if (fh.isFileHeader() && fh.isUnicode()) {
	    name = fh.getFileNameW();
	} else {
	    name = fh.getFileNameString();
	}
	f = new File(destination, name);
	if (!f.exists()) {
	    try {
		f = makeFile(destination, name);
	    } catch (IOException e) {
		logger.error("error creating the new file: " + f.getName(), e);
	    }
	}
	return f;
    }

    private static File makeFile(File destination, String name)
	    throws IOException {
	String[] dirs = name.split("\\\\");
	if (dirs == null) {
	    return null;
	}
	String path = "";
	int size = dirs.length;
	if (size == 1) {
	    return new File(destination, name);
	} else if (size > 1) {
	    for (int i = 0; i < dirs.length - 1; i++) {
		path = path + File.separator + dirs[i];
		new File(destination, path).mkdir();
	    }
	    path = path + File.separator + dirs[dirs.length - 1];
	    File f = new File(destination, path);
	    f.createNewFile();
	    return f;
	} else {
	    return null;
	}
    }

    private static void createDirectory(FileHeader fh, File destination) {
	File f = null;
	if (fh.isDirectory() && fh.isUnicode()) {
	    f = new File(destination, fh.getFileNameW());
	    if (!f.exists()) {
		makeDirectory(destination, fh.getFileNameW());
	    }
	} else if (fh.isDirectory() && !fh.isUnicode()) {
	    f = new File(destination, fh.getFileNameString());
	    if (!f.exists()) {
		makeDirectory(destination, fh.getFileNameString());
	    }
	}
    }

    private static void makeDirectory(File destination, String fileName) {
	String[] dirs = fileName.split("\\\\");
	if (dirs == null) {
	    return;
	}
	String path = "";
	for (String dir : dirs) {
	    path = path + File.separator + dir;
	    new File(destination, path).mkdir();
	}

    }

download  show line numbers  debug dex  old transpilations   

Travelled to 13 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1008995
Snippet name: Extract RAR [WORKS]
Eternal ID of this version: #1008995/4
Text MD5: 2b22636eb75fc30f2a3065f48db3b429
Transpilation MD5: 06468852c8b7c6dfb6e559a90823599b
Author: stefan
Category: javax / i.o.
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2017-06-23 17:42:51
Source code size: 4291 bytes / 162 lines
Pitched / IR pitched: No / No
Views / Downloads: 484 / 521
Version history: 3 change(s)
Referenced in: [show references]