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).

1  
!7
2  
3  
lib 1008994 // junrar
4  
lib 1003619 // apache logging
5  
6  
import org.apache.commons.logging.Log;
7  
import org.apache.commons.logging.LogFactory;
8  
9  
import com.github.junrar.Archive;
10  
import com.github.junrar.exception.RarException;
11  
import com.github.junrar.rarfile.FileHeader;
12  
13  
/**
14  
 * extract an archive to the given location
15  
 * 
16  
 * @author edmund wagner
17  
 * 
18  
 */
19  
20  
    private static Log logger = LogFactory.getLog(main.class
21  
	    .getName());
22  
23  
    public static void extractArchive(String archive, String destination) {
24  
	if (archive == null || destination == null) {
25  
	    throw new RuntimeException("archive and destination must me set");
26  
	}
27  
	File arch = new File(archive);
28  
	if (!arch.exists()) {
29  
	    throw new RuntimeException("the archive does not exit: " + archive);
30  
	}
31  
	File dest = new File(destination);
32  
	if (!dest.exists() || !dest.isDirectory()) {
33  
	    throw new RuntimeException(
34  
		    "the destination must exist and point to a directory: "
35  
			    + destination);
36  
	}
37  
	extractArchive(arch, dest);
38  
    }
39  
40  
    p {
41  
	if (args.length == 2) {
42  
	    extractArchive(args[0], args[1]);
43  
	} else {
44  
	    System.out
45  
		    .println("usage: java -jar extractArchive.jar <thearchive> <the destination directory>");
46  
	}
47  
    }
48  
49  
    public static void extractArchive(File archive, File destination) {
50  
	Archive arch = null;
51  
	try {
52  
	    arch = new Archive(archive);
53  
	} catch (RarException e) {
54  
	    logger.error(e);
55  
	} catch (IOException e1) {
56  
	    logger.error(e1);
57  
	}
58  
	if (arch != null) {
59  
	    if (arch.isEncrypted()) {
60  
		logger.warn("archive is encrypted cannot extreact");
61  
		return;
62  
	    }
63  
	    FileHeader fh = null;
64  
	    while (true) {
65  
		fh = arch.nextFileHeader();
66  
		if (fh == null) {
67  
		    break;
68  
		}
69  
		if (fh.isEncrypted()) {
70  
		    logger.warn("file is encrypted cannot extract: "
71  
			    + fh.getFileNameString());
72  
		    continue;
73  
		}
74  
		logger.info("extracting: " + fh.getFileNameString());
75  
		try {
76  
		    if (fh.isDirectory()) {
77  
			createDirectory(fh, destination);
78  
		    } else {
79  
			File f = createFile(fh, destination);
80  
			OutputStream stream = new FileOutputStream(f);
81  
			arch.extractFile(fh, stream);
82  
			stream.close();
83  
		    }
84  
		} catch (IOException e) {
85  
		    logger.error("error extracting the file", e);
86  
		} catch (RarException e) {
87  
		    logger.error("error extraction the file", e);
88  
		}
89  
	    }
90  
	}
91  
    }
92  
93  
    private static File createFile(FileHeader fh, File destination) {
94  
	File f = null;
95  
	String name = null;
96  
	if (fh.isFileHeader() && fh.isUnicode()) {
97  
	    name = fh.getFileNameW();
98  
	} else {
99  
	    name = fh.getFileNameString();
100  
	}
101  
	f = new File(destination, name);
102  
	if (!f.exists()) {
103  
	    try {
104  
		f = makeFile(destination, name);
105  
	    } catch (IOException e) {
106  
		logger.error("error creating the new file: " + f.getName(), e);
107  
	    }
108  
	}
109  
	return f;
110  
    }
111  
112  
    private static File makeFile(File destination, String name)
113  
	    throws IOException {
114  
	String[] dirs = name.split("\\\\");
115  
	if (dirs == null) {
116  
	    return null;
117  
	}
118  
	String path = "";
119  
	int size = dirs.length;
120  
	if (size == 1) {
121  
	    return new File(destination, name);
122  
	} else if (size > 1) {
123  
	    for (int i = 0; i < dirs.length - 1; i++) {
124  
		path = path + File.separator + dirs[i];
125  
		new File(destination, path).mkdir();
126  
	    }
127  
	    path = path + File.separator + dirs[dirs.length - 1];
128  
	    File f = new File(destination, path);
129  
	    f.createNewFile();
130  
	    return f;
131  
	} else {
132  
	    return null;
133  
	}
134  
    }
135  
136  
    private static void createDirectory(FileHeader fh, File destination) {
137  
	File f = null;
138  
	if (fh.isDirectory() && fh.isUnicode()) {
139  
	    f = new File(destination, fh.getFileNameW());
140  
	    if (!f.exists()) {
141  
		makeDirectory(destination, fh.getFileNameW());
142  
	    }
143  
	} else if (fh.isDirectory() && !fh.isUnicode()) {
144  
	    f = new File(destination, fh.getFileNameString());
145  
	    if (!f.exists()) {
146  
		makeDirectory(destination, fh.getFileNameString());
147  
	    }
148  
	}
149  
    }
150  
151  
    private static void makeDirectory(File destination, String fileName) {
152  
	String[] dirs = fileName.split("\\\\");
153  
	if (dirs == null) {
154  
	    return;
155  
	}
156  
	String path = "";
157  
	for (String dir : dirs) {
158  
	    path = path + File.separator + dir;
159  
	    new File(destination, path).mkdir();
160  
	}
161  
162  
    }

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: 564 / 620
Version history: 3 change(s)
Referenced in: [show references]