package com.getkeepsafe.relinker; import android.content.Context; import android.content.pm.ApplicationInfo; import android.os.Build; import com.getkeepsafe.relinker.ReLinker; import com.ubt.jimu.base.util.FileUtil; import java.io.Closeable; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.zip.ZipEntry; import java.util.zip.ZipFile; /* loaded from: classes.dex */ public class ApkLibraryInstaller implements ReLinker.LibraryInstaller { private static class ZipFileInZipEntry { public ZipFile a; public ZipEntry b; public ZipFileInZipEntry(ZipFile zipFile, ZipEntry zipEntry) { this.a = zipFile; this.b = zipEntry; } } private String[] a(Context context) { String[] strArr; ApplicationInfo applicationInfo = context.getApplicationInfo(); if (Build.VERSION.SDK_INT < 21 || (strArr = applicationInfo.splitSourceDirs) == null || strArr.length == 0) { return new String[]{applicationInfo.sourceDir}; } String[] strArr2 = new String[strArr.length + 1]; strArr2[0] = applicationInfo.sourceDir; System.arraycopy(strArr, 0, strArr2, 1, strArr.length); return strArr2; } private ZipFileInZipEntry a(Context context, String[] strArr, String str, ReLinkerInstance reLinkerInstance) { int i; String[] a = a(context); int length = a.length; char c = 0; ZipFile zipFile = null; int i2 = 0; while (i2 < length) { String str2 = a[i2]; int i3 = 0; while (true) { int i4 = i3 + 1; i = 5; if (i3 >= 5) { break; } try { zipFile = new ZipFile(new File(str2), 1); break; } catch (IOException unused) { i3 = i4; } } if (zipFile != null) { int i5 = 0; while (true) { int i6 = i5 + 1; if (i5 < i) { int length2 = strArr.length; int i7 = 0; while (i7 < length2) { String str3 = "lib" + File.separatorChar + strArr[i7] + File.separatorChar + str; Object[] objArr = new Object[2]; objArr[c] = str3; objArr[1] = str2; reLinkerInstance.a("Looking for %s in APK %s...", objArr); ZipEntry entry = zipFile.getEntry(str3); if (entry != null) { return new ZipFileInZipEntry(zipFile, entry); } i7++; c = 0; } i5 = i6; i = 5; } else { try { zipFile.close(); break; } catch (IOException unused2) { } } } } i2++; c = 0; } return null; } @Override // com.getkeepsafe.relinker.ReLinker.LibraryInstaller public void a(Context context, String[] strArr, String str, File file, ReLinkerInstance reLinkerInstance) { ZipFileInZipEntry zipFileInZipEntry; InputStream inputStream; FileOutputStream fileOutputStream; long a; Closeable closeable = null; try { zipFileInZipEntry = a(context, strArr, str, reLinkerInstance); try { if (zipFileInZipEntry == null) { throw new MissingLibraryException(str); } int i = 0; while (true) { int i2 = i + 1; if (i < 5) { reLinkerInstance.a("Found %s! Extracting...", str); try { if (file.exists() || file.createNewFile()) { try { inputStream = zipFileInZipEntry.a.getInputStream(zipFileInZipEntry.b); try { fileOutputStream = new FileOutputStream(file); try { a = a(inputStream, fileOutputStream); fileOutputStream.getFD().sync(); } catch (FileNotFoundException unused) { a(inputStream); a(fileOutputStream); i = i2; } catch (IOException unused2) { a(inputStream); a(fileOutputStream); i = i2; } catch (Throwable th) { th = th; closeable = fileOutputStream; a(inputStream); a(closeable); throw th; } } catch (FileNotFoundException unused3) { fileOutputStream = null; } catch (IOException unused4) { fileOutputStream = null; } catch (Throwable th2) { th = th2; } } catch (FileNotFoundException unused5) { inputStream = null; fileOutputStream = null; } catch (IOException unused6) { inputStream = null; fileOutputStream = null; } catch (Throwable th3) { th = th3; inputStream = null; } if (a != file.length()) { a(inputStream); a(fileOutputStream); } else { a(inputStream); a(fileOutputStream); file.setReadable(true, false); file.setExecutable(true, false); file.setWritable(true); if (zipFileInZipEntry != null) { try { if (zipFileInZipEntry.a != null) { zipFileInZipEntry.a.close(); return; } return; } catch (IOException unused7) { return; } } return; } } } catch (IOException unused8) { } i = i2; } else { reLinkerInstance.a("FATAL! Couldn't extract the library from the APK!"); if (zipFileInZipEntry != null) { try { if (zipFileInZipEntry.a != null) { zipFileInZipEntry.a.close(); return; } return; } catch (IOException unused9) { return; } } return; } } } catch (Throwable th4) { th = th4; if (zipFileInZipEntry != null) { try { if (zipFileInZipEntry.a != null) { zipFileInZipEntry.a.close(); } } catch (IOException unused10) { } } throw th; } } catch (Throwable th5) { th = th5; zipFileInZipEntry = null; } } private long a(InputStream inputStream, OutputStream outputStream) throws IOException { byte[] bArr = new byte[FileUtil.ZIP_BUFFER_SIZE]; long j = 0; while (true) { int read = inputStream.read(bArr); if (read == -1) { outputStream.flush(); return j; } outputStream.write(bArr, 0, read); j += read; } } private void a(Closeable closeable) { if (closeable != null) { try { closeable.close(); } catch (IOException unused) { } } } }