{"record":{"id":"be6b9e08ca8f20c0","repo":"MuntashirAkon/AppManager","slug":"malformed-apk-not-a-zip-archive","errorCode":null,"errorMessage":"Malformed APK: not a ZIP archive","messagePattern":"Malformed APK: not a ZIP archive","errorType":"exception","errorClass":"ApkFile.ApkFileException","httpStatus":null,"severity":"error","filePath":"app/src/main/java/io/github/muntashirakon/AppManager/apk/ApkUtils.java","lineNumber":155,"sourceCode":"        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {\n            return apkName.replaceAll(\"%min_sdk%\", String.valueOf(packageInfo.applicationInfo.minSdkVersion));\n        }\n        return apkName;\n    }\n\n    public static boolean isSplitApk(@NonNull ApplicationInfo info) {\n        return info.splitPublicSourceDirs != null && info.splitPublicSourceDirs.length > 0;\n    }\n\n    @NonNull\n    public static ByteBuffer getManifestFromApk(File apkFile) throws ApkFile.ApkFileException {\n        try (RandomAccessFile in = new RandomAccessFile(apkFile, \"r\")) {\n            DataSource apk = DataSources.asDataSource(in);\n            com.android.apksig.apk.ApkUtils.ZipSections apkSections;\n            try {\n                apkSections = com.android.apksig.apk.ApkUtils.findZipSections(apk);\n            } catch (ZipFormatException e) {\n                throw new ApkFile.ApkFileException(\"Malformed APK: not a ZIP archive\", e);\n            }\n            List<CentralDirectoryRecord> cdRecords;\n            try {\n                cdRecords = ZipUtils.parseZipCentralDirectory(apk, apkSections);\n            } catch (ApkFormatException e) {\n                throw new ApkFile.ApkFileException(e.getMessage(), e);\n            }\n            try {\n                return getAndroidManifestFromApk(\n                        cdRecords,\n                        apk.slice(0, apkSections.getZipCentralDirectoryOffset()));\n            } catch (ApkFormatException e) {\n                throw new ApkFile.ApkFileException(e.getMessage(), e);\n            } catch (ZipFormatException e) {\n                throw new ApkFile.ApkFileException(\"Failed to read \" + MANIFEST_FILE, e);\n            }\n        } catch (IOException e) {\n            throw new ApkFile.ApkFileException(e.getMessage(), e);","sourceCodeStart":137,"sourceCodeEnd":173,"githubUrl":"https://github.com/MuntashirAkon/AppManager/blob/0152f468fc9463ee02dc2ca83f6fe4989a2c4ca5/app/src/main/java/io/github/muntashirakon/AppManager/apk/ApkUtils.java#L137-L173","documentation":"getManifestFromApk parses the APK with apksig's ZIP machinery; when findZipSections cannot locate valid End-of-Central-Directory records, the file is not a ZIP and this ApkFileException is thrown with the message 'Malformed APK: not a ZIP archive'.","triggerScenarios":"Passing a File to getManifestFromApk(File) whose content is not a ZIP archive — findZipSections throws ZipFormatException.","commonSituations":"Download truncated/interrupted APK, HTML error page saved as .apk, base64 or PEM file mislabeled, corrupt cache, or a split-APK base blob that is actually not a zip.","solutions":["Check the file starts with the ZIP magic bytes 'PK' (0x50 0x4B) before parsing","Re-download or re-obtain the APK file","Verify the file size matches the expected size / checksum","Check logs of whatever produced the file (download manager, cache) for truncation"],"exampleFix":"// before\ngetManifestFromApk(suspiciousFile);\n// after\ntry (RandomAccessFile raf = new RandomAccessFile(suspiciousFile, \"r\")) {\n    byte[] magic = new byte[2];\n    raf.readFully(magic);\n    if (magic[0] != 'P' || magic[1] != 'K') throw new IllegalArgumentException(\"Not a ZIP/APK\");\n}\ngetManifestFromApk(suspiciousFile);","handlingStrategy":"validation","validationCode":"static boolean looksLikeZip(File f) throws IOException {\n    try (RandomAccessFile raf = new RandomAccessFile(f, \"r\")) {\n        byte[] magic = new byte[2];\n        raf.readFully(magic);\n        return magic[0] == 'P' && magic[1] == 'K';\n    }\n}","typeGuard":null,"tryCatchPattern":"try {\n    return ApkUtils.getManifestFromApk(apkFile);\n} catch (ApkFile.ApkFileException e) {\n    if (e.getMessage() != null && e.getMessage().contains(\"not a ZIP\")) {\n        // re-download / reject the file\n    }\n    throw e;\n}","preventionTips":["Verify checksums/sizes of downloaded APK files","Never treat HTML error pages or split blobs as APK files","Check 'PK' magic bytes before parsing"],"tags":["apk","zip","file-validation"],"backgroundTag":"invalid-argument-format","analyzedSha":"0152f468fc9463ee02dc2ca83f6fe4989a2c4ca5","analyzedAt":"2026-09-12T14:03:37.243Z","contentChangedAt":"2026-09-12T14:03:37.243Z","schemaVersion":2},"datasetVersion":"2026-09-14T16:17:12.679Z"}