package com.tencent.mm.a;

import com.tencent.ytcommon.BuildConfig;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.zip.ZipOutputStream;

public final class q {
    public static void a(Collection<File> collection, File file) {
        ZipOutputStream zipOutputStream = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(file), 131072));
        for (File file2 : collection) {
            if (file2.exists()) {
                a(file2, zipOutputStream, BuildConfig.FLAVOR);
            }
        }
        zipOutputStream.close();
    }

    /* JADX WARNING: Removed duplicated region for block: B:28:0x0086  */
    /* JADX WARNING: Removed duplicated region for block: B:22:0x0074  */
    private static void a(java.io.File r6, java.util.zip.ZipOutputStream r7, java.lang.String r8) {
        /*
        r4 = 131072; // 0x20000 float:1.83671E-40 double:6.47582E-319;
        r1 = 0;
        r0 = new java.lang.StringBuilder;
        r0.<init>();
        r2 = r0.append(r8);
        r0 = r8.trim();
        r0 = r0.length();
        if (r0 != 0) goto L_0x003f;
    L_0x0016:
        r0 = "";
    L_0x0019:
        r0 = r2.append(r0);
        r2 = r6.getName();
        r0 = r0.append(r2);
        r3 = r0.toString();
        r0 = r6.isDirectory();
        if (r0 == 0) goto L_0x0042;
    L_0x002f:
        r2 = r6.listFiles();
        r4 = r2.length;
        r0 = r1;
    L_0x0035:
        if (r0 >= r4) goto L_0x007d;
    L_0x0037:
        r1 = r2[r0];
        a(r1, r7, r3);
        r0 = r0 + 1;
        goto L_0x0035;
    L_0x003f:
        r0 = java.io.File.separator;
        goto L_0x0019;
    L_0x0042:
        r0 = new byte[r4];
        r2 = 0;
        r1 = new java.io.BufferedInputStream;	 Catch:{ Exception -> 0x008c, all -> 0x0082 }
        r4 = new java.io.FileInputStream;	 Catch:{ Exception -> 0x008c, all -> 0x0082 }
        r4.<init>(r6);	 Catch:{ Exception -> 0x008c, all -> 0x0082 }
        r5 = 131072; // 0x20000 float:1.83671E-40 double:6.47582E-319;
        r1.<init>(r4, r5);	 Catch:{ Exception -> 0x008c, all -> 0x0082 }
        r2 = new java.util.zip.ZipEntry;	 Catch:{ Exception -> 0x0065 }
        r2.<init>(r3);	 Catch:{ Exception -> 0x0065 }
        r7.putNextEntry(r2);	 Catch:{ Exception -> 0x0065 }
    L_0x0059:
        r2 = r1.read(r0);	 Catch:{ Exception -> 0x0065 }
        r3 = -1;
        if (r2 == r3) goto L_0x007e;
    L_0x0060:
        r3 = 0;
        r7.write(r0, r3, r2);	 Catch:{ Exception -> 0x0065 }
        goto L_0x0059;
    L_0x0065:
        r0 = move-exception;
    L_0x0066:
        r2 = "MicroMsg.ZipUtil";
        r3 = "";
        r4 = 0;
        r4 = new java.lang.Object[r4];	 Catch:{ all -> 0x008a }
        com.tencent.mm.sdk.platformtools.ab.printErrStackTrace(r2, r0, r3, r4);	 Catch:{ all -> 0x008a }
        if (r1 == 0) goto L_0x0077;
    L_0x0074:
        r1.close();
    L_0x0077:
        r7.flush();
        r7.closeEntry();
    L_0x007d:
        return;
    L_0x007e:
        r1.close();
        goto L_0x0077;
    L_0x0082:
        r0 = move-exception;
        r1 = r2;
    L_0x0084:
        if (r1 == 0) goto L_0x0089;
    L_0x0086:
        r1.close();
    L_0x0089:
        throw r0;
    L_0x008a:
        r0 = move-exception;
        goto L_0x0084;
    L_0x008c:
        r0 = move-exception;
        r1 = r2;
        goto L_0x0066;
        */
        throw new UnsupportedOperationException("Method not decompiled: com.tencent.mm.a.q.a(java.io.File, java.util.zip.ZipOutputStream, java.lang.String):void");
    }

    public static String b(File file, boolean z, String str) {
        int i = 0;
        if (str != null) {
            File parentFile = new File(str).getParentFile();
            if (!(parentFile == null || parentFile.exists())) {
                parentFile.mkdirs();
            }
        }
        ArrayList arrayList = new ArrayList();
        if (file.isDirectory()) {
            File[] listFiles = file.listFiles();
            if (listFiles == null) {
                return null;
            }
            for (Object add : listFiles) {
                arrayList.add(add);
            }
            if (str == null) {
                str = file.getAbsolutePath() + File.separator + file.getName() + ".zip";
            }
        } else {
            arrayList.add(file);
            if (str == null) {
                str = file.getAbsoluteFile().getParentFile().getAbsolutePath() + File.separator + file.getName() + ".zip";
            }
        }
        File file2 = new File(str);
        if (file2.exists()) {
            file2.delete();
        }
        try {
            a(arrayList, file2);
            if (z) {
                while (i < arrayList.size()) {
                    ((File) arrayList.get(i)).delete();
                    i++;
                }
            }
            return file2.getAbsolutePath();
        } catch (Exception e) {
            return null;
        }
    }
}
