package defpackage;

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import net.lingala.zip4j.exception.ZipException;

/* JADX INFO: renamed from: ˏˌᵶˈ, reason: contains not printable characters */
/* JADX INFO: loaded from: classes3.dex */
public final class C1852 extends ᵜᵪʾ {

    /* JADX INFO: renamed from: ᵔᵖᵹᵫ, reason: contains not printable characters */
    public final ᵣˊˈᵪ f7325;

    /* JADX INFO: renamed from: ᵜᵪʾ, reason: contains not printable characters */
    public final C1411 f7326;

    public C1852(C1411 c1411, ᵣˊˈᵪ r2, C1014 c1014) {
        super(c1014);
        this.f7326 = c1411;
        this.f7325 = r2;
    }

    /* JADX INFO: renamed from: ˌᵨʠᵫ, reason: contains not printable characters */
    public static void m5046(File file, File file2, boolean z) throws ZipException {
        if (!z) {
            if (file2.delete()) {
                return;
            }
            C4468.m9508("Could not delete temporary file");
        } else if (!file.delete()) {
            C4468.m9508("cannot delete old zip file");
        } else {
            if (file2.renameTo(file)) {
                return;
            }
            C4468.m9508("cannot rename modified zip file");
        }
    }

    /* JADX INFO: renamed from: ᵸˆˁ, reason: contains not printable characters */
    public static int m5047(ArrayList arrayList, C1543 c1543) throws ZipException {
        for (int i = 0; i < arrayList.size(); i++) {
            if (((C1543) arrayList.get(i)).equals(c1543)) {
                return i;
            }
        }
        C4468.m9508("Could not find file header in list of central directory file headers");
        return 0;
    }

    /* JADX INFO: renamed from: ˑˊᵲ, reason: contains not printable characters */
    public final void m5048(ArrayList arrayList, C1543 c1543, long j) throws ZipException {
        C1411 c1411;
        C0597 c0597;
        if (j == Long.MIN_VALUE) {
            throw new ArithmeticException("long overflow");
        }
        long j2 = -j;
        int iM5047 = m5047(arrayList, c1543);
        if (iM5047 == -1) {
            C4468.m9508("Could not locate modified file header in zipModel");
            return;
        }
        while (true) {
            iM5047++;
            int size = arrayList.size();
            c1411 = this.f7326;
            if (iM5047 >= size) {
                break;
            }
            C1543 c15432 = (C1543) arrayList.get(iM5047);
            c15432.f6210 += j2;
            if (c1411.f5826 && (c0597 = c15432.f15137) != null) {
                long j3 = c0597.f2909;
                if (j3 != -1) {
                    c0597.f2909 = j3 + j2;
                }
            }
        }
        C2078 c2078 = c1411.f5821;
        c2078.f8261 -= j;
        c2078.f8266--;
        int i = c2078.f8265;
        if (i > 0) {
            c2078.f8265 = i - 1;
        }
        if (c1411.f5826) {
            C4148 c4148 = c1411.f5824;
            c4148.f15827 -= j;
            c4148.f15829 = c4148.f15835 - 1;
            c1411.f5823.f6637 -= j;
        }
    }

    /* JADX INFO: renamed from: ᵶᵥᵝ, reason: contains not printable characters */
    public final void m5049(ᵜᵪʾ r25, C3442 c3442) throws Throwable {
        boolean z;
        Throwable th;
        ArrayList arrayList;
        long j;
        int i;
        long j2;
        ArrayList arrayList2;
        C0835 c0835 = (C0835) r25;
        C1411 c1411 = this.f7326;
        if (c1411.f5825) {
            C4468.m9508("This is a split archive. Zip file format does not allow updating split/spanned files");
            return;
        }
        List<String> list = c0835.f3732;
        C3185 c3185 = (C3185) ((ᵜᵪʾ) c0835).ˌᴵ;
        ArrayList arrayList3 = new ArrayList();
        for (String str : list) {
            if (ᵬʕˎʽ.ᵪᵹᵥᵻ(c1411, str) != null) {
                arrayList3.add(str);
            }
        }
        if (arrayList3.isEmpty()) {
            return;
        }
        String path = c1411.f5820.getPath();
        SecureRandom secureRandom = new SecureRandom();
        StringBuilder sbM8219 = AbstractC3653.m8219(path);
        sbM8219.append(secureRandom.nextInt(10000));
        File file = new File(sbM8219.toString());
        while (file.exists()) {
            StringBuilder sbM82192 = AbstractC3653.m8219(path);
            sbM82192.append(secureRandom.nextInt(10000));
            file = new File(sbM82192.toString());
        }
        try {
            C4908 c4908 = new C4908(file);
            try {
                RandomAccessFile randomAccessFile = new RandomAccessFile(c1411.f5820, "r");
                try {
                    ArrayList arrayList4 = new ArrayList(c1411.f5822.f1746);
                    Collections.sort(arrayList4, new C4170(0));
                    int size = arrayList4.size();
                    int i2 = 0;
                    long j3 = 0;
                    while (i2 < size) {
                        Object obj = arrayList4.get(i2);
                        i2++;
                        C1543 c1543 = (C1543) obj;
                        int iM5047 = m5047(arrayList4, c1543);
                        if (iM5047 != arrayList4.size() - 1) {
                            arrayList = arrayList3;
                            j = ((C1543) arrayList4.get(iM5047 + 1)).f6210;
                        } else if (c1411.f5826) {
                            arrayList = arrayList3;
                            j = c1411.f5824.f15827;
                        } else {
                            arrayList = arrayList3;
                            j = c1411.f5821.f8261;
                        }
                        long filePointer = j - c4908.f18681.getFilePointer();
                        int size2 = arrayList.size();
                        int i3 = 0;
                        while (i3 < size2) {
                            i = size;
                            ArrayList arrayList5 = arrayList;
                            Object obj2 = arrayList5.get(i3);
                            i3++;
                            arrayList = arrayList5;
                            String str2 = (String) obj2;
                            int i4 = size2;
                            if ((!str2.endsWith("/") || !c1543.f15143.startsWith(str2)) && !c1543.f15143.equals(str2)) {
                                size = i;
                                size2 = i4;
                            }
                            m5048(arrayList4, c1543, filePointer);
                            if (!c1411.f5822.f1746.remove(c1543)) {
                                throw new ZipException("Could not remove entry from list of central directory headers");
                            }
                            j3 += filePointer;
                            arrayList2 = arrayList4;
                            ˌᴵ();
                            arrayList3 = arrayList;
                            arrayList4 = arrayList2;
                            size = i;
                        }
                        i = size;
                        int i5 = c3185.f12092;
                        long j4 = j3 + filePointer;
                        if (j3 < 0 || j4 < 0 || j3 > j4) {
                            j2 = filePointer;
                            arrayList2 = arrayList4;
                            C4468.m9508("invalid offsets");
                        } else if (j3 == j4) {
                            j2 = filePointer;
                            arrayList2 = arrayList4;
                        } else {
                            try {
                                randomAccessFile.seek(j3);
                                long j5 = j4 - j3;
                                j2 = filePointer;
                                byte[] bArr = j5 < ((long) i5) ? new byte[(int) j5] : new byte[i5];
                                long j6 = 0;
                                while (true) {
                                    int i6 = randomAccessFile.read(bArr);
                                    if (i6 == -1) {
                                        arrayList2 = arrayList4;
                                        break;
                                    }
                                    c4908.write(bArr, 0, i6);
                                    arrayList2 = arrayList4;
                                    long j7 = i6;
                                    c3442.m7858(j7);
                                    j6 += j7;
                                    if (j6 == j5) {
                                        break;
                                    }
                                    if (j6 + ((long) bArr.length) > j5) {
                                        bArr = new byte[(int) (j5 - j6)];
                                    }
                                    arrayList4 = arrayList2;
                                }
                            } catch (IOException e) {
                                throw new ZipException(e);
                            }
                        }
                        j3 += j2;
                        ˌᴵ();
                        arrayList3 = arrayList;
                        arrayList4 = arrayList2;
                        size = i;
                    }
                    this.f7325.ᵔᵖᵹᵫ(c1411, c4908);
                    try {
                        randomAccessFile.close();
                        try {
                            c4908.close();
                            m5046(c1411.f5820, file, true);
                        } catch (Throwable th2) {
                            th = th2;
                            z = true;
                            m5046(c1411.f5820, file, z);
                            throw th;
                        }
                    } catch (Throwable th3) {
                        th = th3;
                        z = true;
                        try {
                            try {
                                c4908.close();
                                throw th;
                            } catch (Throwable th4) {
                                th.addSuppressed(th4);
                                throw th;
                            }
                        } catch (Throwable th5) {
                            th = th5;
                            m5046(c1411.f5820, file, z);
                            throw th;
                        }
                    }
                } finally {
                }
            } catch (Throwable th6) {
                th = th6;
                z = false;
            }
        } catch (Throwable th7) {
            th = th7;
            z = false;
        }
    }
}
