Android 14 issue | java.util.zip.ZipException: Invalid zip entry path: /res/l3MK | java.lang.SecurityException: Writable dex file ... is not allowed

Getting issue on Android 14 device:

java.util.zip.ZipException: Invalid zip entry path: /res/l3MK 
java.lang.SecurityException: Writable dex file '/data/data/..../base.apk/classes23.dex' is not allowed.

Code as below

public static boolean unzipFile(File sourceFile, File destinationDir) {

        InputStream inputStream = null;
        try {
            inputStream = new FileInputStream(sourceFile);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }

        try (ZipInputStream zipInputStream = new ZipInputStream(inputStream)) {

            // start extaction
            ZipEntry nextEntry;

            byte[] bArr = new byte[2048];

            while ((nextEntry = zipInputStream.getNextEntry()) != null) {

                File file3 = newFile(destinationDir, nextEntry);

                File parentFile = nextEntry.isDirectory() ? file3 : file3.getParentFile();

                assert parentFile != null;

                if (parentFile.isDirectory() || parentFile.mkdirs()) {

                    if (!nextEntry.isDirectory()) {

                        FileOutputStream fileOutputStream = new FileOutputStream(file3);

                        while (true) {
                            int read = zipInputStream.read(bArr);
                            if (read == -1) {
                                break;
                            }
                            fileOutputStream.write(bArr, 0, read);
                        }
                    }
                }
            }
            return true;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
    }

    public static File newFile(File targetPath, ZipEntry zipEntry) throws IOException {
        String name = zipEntry.getName();
        File f = new File(targetPath, name);
        String canonicalPath = f.getCanonicalPath();
        if (!canonicalPath.startsWith(targetPath.getCanonicalPath() + File.separator)) {
            throw new ZipException("Illegal name: " + name);
        }
        return f;
    }

Upvotes: 0

Views: 216

Answers (1)

Warm Man
Warm Man

Reputation: 223

I have found the solution. Please refer to this answer:

https://stackoverflow.com/a/77697327/15149850

Upvotes: 0

Related Questions