package com.adobe.marketing.mobile.internal.util;

import bn.m;
import com.google.firebase.sessions.settings.RemoteSettings;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import kotlin.jvm.internal.t;
import kotlin.text.l;
import kotlin.text.y;
import rm.n0;

/* loaded from: classes4.dex */
public final class c {

    /* renamed from: a, reason: collision with root package name */
    public static final c f12314a = new c();

    private c() {
    }

    public static final void a(File src, File dest) {
        t.g(src, "src");
        t.g(dest, "dest");
        m.i(src, dest, true, 0, 4, null);
    }

    public static final boolean b(File file, boolean z10) {
        boolean j10;
        if (file == null) {
            return false;
        }
        if (!z10) {
            return file.delete();
        }
        j10 = m.j(file);
        return j10;
    }

    public static final boolean c(File file, String outputDirectoryPath) {
        boolean P;
        t.g(outputDirectoryPath, "outputDirectoryPath");
        if (file == null) {
            return false;
        }
        File file2 = new File(outputDirectoryPath);
        if (!file2.exists() && !file2.mkdir()) {
            wg.t.a("MobileCore", "FileUtils", "Could not create the output directory " + outputDirectoryPath, new Object[0]);
            return false;
        }
        try {
            ZipInputStream zipInputStream = new ZipInputStream(new FileInputStream(file));
            try {
                ZipEntry nextEntry = zipInputStream.getNextEntry();
                String outputFolderCanonicalPath = file2.getCanonicalPath();
                if (nextEntry == null) {
                    wg.t.a("MobileCore", "FileUtils", "Zip file was invalid", new Object[0]);
                    bn.c.a(zipInputStream, null);
                    return false;
                }
                boolean z10 = true;
                boolean z11 = true;
                while (nextEntry != null && z10) {
                    File file3 = new File(outputDirectoryPath + File.separator + nextEntry.getName());
                    String canonicalPath = file3.getCanonicalPath();
                    t.f(canonicalPath, "newZipEntryFile.canonicalPath");
                    t.f(outputFolderCanonicalPath, "outputFolderCanonicalPath");
                    P = y.P(canonicalPath, outputFolderCanonicalPath, false, 2, null);
                    if (!P) {
                        wg.t.a("MobileCore", "FileUtils", "The zip file contained an invalid path. Verify that your zip file is formatted correctly and has not been tampered with.", new Object[0]);
                        bn.c.a(zipInputStream, null);
                        return false;
                    }
                    if (nextEntry.isDirectory()) {
                        if (!file3.exists() && !file3.mkdirs()) {
                            z10 = false;
                        }
                        z10 = true;
                    } else {
                        File parentFile = file3.getParentFile();
                        if (parentFile == null || !(parentFile.exists() || parentFile.mkdirs())) {
                            wg.t.a("MobileCore", "FileUtils", "Could not extract the file " + file3.getAbsolutePath(), new Object[0]);
                            bn.c.a(zipInputStream, null);
                            return false;
                        }
                        z10 = h(file3, zipInputStream, false);
                    }
                    z11 = z11 && z10;
                    zipInputStream.closeEntry();
                    nextEntry = zipInputStream.getNextEntry();
                }
                zipInputStream.closeEntry();
                n0 n0Var = n0.f34904a;
                bn.c.a(zipInputStream, null);
                return z11;
            } finally {
            }
        } catch (Exception e10) {
            wg.t.a("MobileCore", "FileUtils", "Extraction failed - " + e10, new Object[0]);
            return false;
        }
    }

    public static final boolean d(File file) {
        if (file != null) {
            try {
                if (file.exists() && file.canRead() && file.isFile()) {
                    return true;
                }
            } catch (SecurityException e10) {
                wg.t.a("MobileCore", "FileUtils", "Failed to read file (" + e10 + ')', new Object[0]);
                return false;
            }
        }
        wg.t.a("MobileCore", "FileUtils", "File does not exist or doesn't have read permission " + file, new Object[0]);
        return false;
    }

    public static final boolean e(File file) {
        return file != null && file.isDirectory() && file.canWrite();
    }

    public static final void f(File src, File dest) {
        t.g(src, "src");
        t.g(dest, "dest");
        if (dest.getParentFile() != null && !dest.getParentFile().exists()) {
            dest.getParentFile().mkdirs();
        }
        if (!dest.exists()) {
            dest.createNewFile();
        }
        a(src, dest);
        b(src, false);
    }

    public static final String g(File file) {
        if (!d(file)) {
            wg.t.a("MobileCore", "FileUtils", "Failed to read file: (" + file + ')', new Object[0]);
            return null;
        }
        StringBuilder sb2 = new StringBuilder();
        try {
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(file), kotlin.text.d.f26484b));
            while (true) {
                try {
                    String readLine = bufferedReader.readLine();
                    if (readLine == null) {
                        n0 n0Var = n0.f34904a;
                        bn.c.a(bufferedReader, null);
                        return sb2.toString();
                    }
                    sb2.append(readLine);
                } finally {
                }
            }
        } catch (Exception e10) {
            wg.t.a("MobileCore", "FileUtils", "Failed to read " + file + " contents. " + e10, new Object[0]);
            return null;
        }
    }

    public static final boolean h(File file, InputStream inputStream, boolean z10) {
        t.g(inputStream, "inputStream");
        try {
            FileOutputStream fileOutputStream = new FileOutputStream(file, z10);
            try {
                bn.b.a(inputStream, fileOutputStream, 4096);
                bn.c.a(fileOutputStream, null);
                return true;
            } finally {
            }
        } catch (Exception e10) {
            StringBuilder sb2 = new StringBuilder();
            sb2.append("Unexpected exception while attempting to write to file: ");
            sb2.append(file != null ? file.getPath() : null);
            sb2.append(" (");
            sb2.append(e10);
            sb2.append(')');
            wg.t.a("MobileCore", "FileUtils", sb2.toString(), new Object[0]);
            return false;
        }
    }

    public static final String i(String filePath) {
        boolean C;
        t.g(filePath, "filePath");
        C = y.C(filePath);
        if (C) {
            return filePath;
        }
        return new l(RemoteSettings.FORWARD_SLASH_STRING).g(new l("[/\\\\](\\.{2,})").g(new l("\\.[/\\\\]").g(filePath, "\\."), "_"), "");
    }
}
