1 package edu.jiangxin.apktoolbox.file.password.recovery.checker;
2
3 import com.github.junrar.Archive;
4 import com.github.junrar.exception.CrcErrorException;
5 import com.github.junrar.exception.RarException;
6 import com.github.junrar.exception.UnsupportedRarVersionException;
7 import com.github.junrar.exception.WrongPasswordException;
8 import com.github.junrar.rarfile.FileHeader;
9 import edu.jiangxin.apktoolbox.file.password.recovery.exception.UnknownException;
10 import edu.jiangxin.apktoolbox.file.password.recovery.exception.UnsupportedVersionException;
11
12 import java.io.IOException;
13 import java.io.OutputStream;
14
15 public final class RarChecker extends FileChecker {
16 private static final boolean DEBUG = false;
17
18 public RarChecker() {
19 super();
20 }
21
22 @Override
23 public String[] getFileExtensions() {
24 return new String[]{"rar"};
25 }
26
27 @Override
28 public String getFileDescription() {
29 return "*.rar";
30 }
31
32 @Override
33 public String getDescription() {
34 return "RAR Checker(RAR1.4–7)";
35 }
36
37 @Override
38 public boolean prepareChecker() {
39 return true;
40 }
41
42 @Override
43 public boolean checkPassword(String password) {
44 boolean result = false;
45 try (Archive archive = new Archive(file, password)) {
46 while (true) {
47 FileHeader fileHeader = archive.nextFileHeader();
48 if (fileHeader == null) {
49 break;
50 }
51 archive.extractFile(fileHeader, new OutputStream() {
52 @Override
53 public void write(int b) {
54 }
55 });
56 }
57 result = true;
58 } catch (CrcErrorException e) {
59 if (DEBUG) {
60 logger.error("[CrcErrorException]password is incorrect: {}", password);
61 }
62 } catch (UnsupportedRarVersionException e) {
63 throw new UnsupportedVersionException(e);
64 } catch (WrongPasswordException e) {
65 if (DEBUG) {
66 logger.error("[WrongPasswordException]password is incorrect: {}", password);
67 }
68 } catch (RarException e) {
69 if (DEBUG) {
70 logger.error("[RarException]password is incorrect: {}", password);
71 }
72 } catch (IOException e) {
73 throw new UnknownException(e);
74 }
75 return result;
76 }
77 }