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;

/* renamed from: ˁʤᵼ, reason: contains not printable characters */
/* loaded from: classes3.dex */
public final class C0639 extends ᵢˇᵭ {

    /* renamed from: ᵢˇᵭ, reason: contains not printable characters */
    public final C4348 f3147;

    /* renamed from: ᵿʗᵝʽ, reason: contains not printable characters */
    public final ᵧˉʙ f3148;

    public C0639(C4348 c4348, ᵧˉʙ r2, C3718 c3718) {
        super(c3718);
        this.f3147 = c4348;
        this.f3148 = r2;
    }

    /* renamed from: ˑᵥˁ, reason: contains not printable characters */
    public static int m2524(ArrayList arrayList, C1451 c1451) throws ZipException {
        for (int i = 0; i < arrayList.size(); i++) {
            if (((C1451) arrayList.get(i)).equals(c1451)) {
                return i;
            }
        }
        C0190.m1425("Could not find file header in list of central directory file headers");
        return 0;
    }

    /* renamed from: ᵡˎ, reason: contains not printable characters */
    public static void m2525(File file, File file2, boolean z) throws ZipException {
        if (!z) {
            if (file2.delete()) {
                return;
            }
            C0190.m1425("Could not delete temporary file");
        } else if (!file.delete()) {
            C0190.m1425("cannot delete old zip file");
        } else {
            if (file2.renameTo(file)) {
                return;
            }
            C0190.m1425("cannot rename modified zip file");
        }
    }

    /* renamed from: ˎˑː, reason: contains not printable characters */
    public final void m2526(ArrayList arrayList, C1451 c1451, long j) throws ZipException {
        C4348 c4348;
        C3062 c3062;
        if (j == Long.MIN_VALUE) {
            throw new ArithmeticException("long overflow");
        }
        long j2 = -j;
        int iM2524 = m2524(arrayList, c1451);
        if (iM2524 == -1) {
            C0190.m1425("Could not locate modified file header in zipModel");
            return;
        }
        while (true) {
            iM2524++;
            int size = arrayList.size();
            c4348 = this.f3147;
            if (iM2524 >= size) {
                break;
            }
            C1451 c14512 = (C1451) arrayList.get(iM2524);
            c14512.f5870 += j2;
            if (c4348.f16270 && (c3062 = c14512.f4031) != null) {
                long j3 = c3062.f11720;
                if (j3 != -1) {
                    c3062.f11720 = j3 + j2;
                }
            }
        }
        C0737 c0737 = c4348.f16277;
        c0737.f3553 -= j;
        c0737.f3556--;
        int i = c0737.f3557;
        if (i > 0) {
            c0737.f3557 = i - 1;
        }
        if (c4348.f16270) {
            C5414 c5414 = c4348.f16274;
            c5414.f20294 -= j;
            c5414.f20291 = c5414.f20290 - 1;
            c4348.f16276.f7746 -= j;
        }
    }

    /* renamed from: ᵸˑ, reason: contains not printable characters */
    public final void m2527(ᵢˇᵭ r25, C3944 c3944) throws Throwable {
        boolean z;
        Throwable th;
        ArrayList arrayList;
        long j;
        int i;
        long j2;
        ArrayList arrayList2;
        C4712 c4712 = (C4712) r25;
        C4348 c4348 = this.f3147;
        if (c4348.f16272) {
            C0190.m1425("This is a split archive. Zip file format does not allow updating split/spanned files");
            return;
        }
        List<String> list = c4712.f17395;
        C2723 c2723 = (C2723) ((ᵢˇᵭ) c4712).ᵧᵾˀˈ;
        ArrayList arrayList3 = new ArrayList();
        for (String str : list) {
            if (ˉᵜˆ.ᵳٴᵶ(c4348, str) != null) {
                arrayList3.add(str);
            }
        }
        if (arrayList3.isEmpty()) {
            return;
        }
        String path = c4348.f16271.getPath();
        SecureRandom secureRandom = new SecureRandom();
        StringBuilder sbM4096 = AbstractC1327.m4096(path);
        sbM4096.append(secureRandom.nextInt(10000));
        File file = new File(sbM4096.toString());
        while (file.exists()) {
            StringBuilder sbM40962 = AbstractC1327.m4096(path);
            sbM40962.append(secureRandom.nextInt(10000));
            file = new File(sbM40962.toString());
        }
        try {
            C4218 c4218 = new C4218(file);
            try {
                RandomAccessFile randomAccessFile = new RandomAccessFile(c4348.f16271, "r");
                try {
                    ArrayList arrayList4 = new ArrayList(c4348.f16273.f11143);
                    Collections.sort(arrayList4, new C4544(0));
                    int size = arrayList4.size();
                    int i2 = 0;
                    long j3 = 0;
                    while (i2 < size) {
                        Object obj = arrayList4.get(i2);
                        i2++;
                        C1451 c1451 = (C1451) obj;
                        int iM2524 = m2524(arrayList4, c1451);
                        if (iM2524 != arrayList4.size() - 1) {
                            arrayList = arrayList3;
                            j = ((C1451) arrayList4.get(iM2524 + 1)).f5870;
                        } else if (c4348.f16270) {
                            arrayList = arrayList3;
                            j = c4348.f16274.f20294;
                        } else {
                            arrayList = arrayList3;
                            j = c4348.f16277.f3553;
                        }
                        long filePointer = j - c4218.f15809.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("/") || !c1451.f4041.startsWith(str2)) && !c1451.f4041.equals(str2)) {
                                size = i;
                                size2 = i4;
                            }
                            m2526(arrayList4, c1451, filePointer);
                            if (!c4348.f16273.f11143.remove(c1451)) {
                                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 = c2723.f10553;
                        long j4 = j3 + filePointer;
                        if (j3 < 0 || j4 < 0 || j3 > j4) {
                            j2 = filePointer;
                            arrayList2 = arrayList4;
                            C0190.m1425("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;
                                    }
                                    c4218.write(bArr, 0, i6);
                                    arrayList2 = arrayList4;
                                    long j7 = i6;
                                    c3944.m8729(j7);
                                    j6 += j7;
                                    if (j6 == j5) {
                                        break;
                                    }
                                    if (j6 + 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.f3148.ˑᵱ(c4348, c4218);
                    try {
                        randomAccessFile.close();
                        try {
                            c4218.close();
                            m2525(c4348.f16271, file, true);
                        } catch (Throwable th2) {
                            th = th2;
                            z = true;
                            m2525(c4348.f16271, file, z);
                            throw th;
                        }
                    } catch (Throwable th3) {
                        th = th3;
                        z = true;
                        try {
                            try {
                                c4218.close();
                                throw th;
                            } catch (Throwable th4) {
                                th.addSuppressed(th4);
                                throw th;
                            }
                        } catch (Throwable th5) {
                            th = th5;
                            m2525(c4348.f16271, file, z);
                            throw th;
                        }
                    }
                } finally {
                }
            } catch (Throwable th6) {
                th = th6;
                z = false;
            }
        } catch (Throwable th7) {
            th = th7;
            z = false;
        }
    }
}
