1 package edu.jiangxin.apktoolbox.file.password.recovery.checker; 2 3 import edu.jiangxin.apktoolbox.file.password.recovery.exception.UnknownException; 4 import net.lingala.zip4j.exception.ZipException; 5 import net.lingala.zip4j.io.inputstream.ZipInputStream; 6 7 import java.io.FileInputStream; 8 import java.io.IOException; 9 import java.nio.charset.StandardCharsets; 10 11 public final class ZipChecker extends FileChecker { 12 private static final boolean DEBUG = false; 13 14 public ZipChecker() { 15 super(); 16 } 17 18 @Override 19 public String[] getFileExtensions() { 20 return new String[]{"zip"}; 21 } 22 23 @Override 24 public String getFileDescription() { 25 return "*.zip"; 26 } 27 28 @Override 29 public String getDescription() { 30 return "ZIP Checker(Not support Non-ASCII password)"; 31 } 32 33 @Override 34 public boolean prepareChecker() { 35 return true; 36 } 37 38 @Override 39 public boolean checkPassword(String password) { 40 if (DEBUG) { 41 logger.info("checkPassword: " + password); 42 } 43 boolean result = false; 44 byte[] readBuffer = new byte[4096]; 45 try (ZipInputStream zipInputStream = new ZipInputStream(new FileInputStream(file), password.toCharArray(), StandardCharsets.UTF_8)) { 46 while (zipInputStream.getNextEntry() != null) { 47 while (zipInputStream.read(readBuffer) != -1) { 48 } 49 } 50 result = true; 51 } catch (ZipException e) { 52 if (DEBUG) { 53 logger.error("checkPassword", e); 54 } 55 } catch (IOException e) { 56 throw new UnknownException(e); 57 } 58 return result; 59 } 60 }