Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get APK package name

Can I get APK package name from uploaded file in JS?

I'm using HTML5 to upload the file and I would like to get the package name of the selected file.

<input type="file"/>

Is it possible?

like image 375
Dor Cohen Avatar asked Sep 27 '22 11:09

Dor Cohen


1 Answers

Finally, I used the amazing JSZip library to get the AndroidManifest.xml content and convert its content to searchable string using the following code:

function getPackageName(apkBlobAsByteArray) {
            // Unzipping zip blob
            var zip = new JSZip(apkBlobAsByteArray);

            // Getting AndroidManifest.xml and decompress it
            var androidCompress = zip.files['AndroidManifest.xml'];
            var androidNonCompress = androidCompress._data.getContent();

            // Reading to content to a searchable string
            var packageNameArray = [];
            var textArray = String(androidNonCompress).split(',');
            for (var i = 0, len = textArray.length; i < len; i++) {
                if (textArray[i] !== 0) {
                    packageNameArray.push(textArray[i]);
                }
            }

            // Searching for package name
            var startPattern = 'manifest';
            var androidText = String.fromCharCode.apply(null, packageNameArray).toString().toLowerCase();
            var packageName = androidText.substring(androidText.indexOf(startPattern) +
                startPattern.length, androidText.indexOf('uses'));
            // Remove version from package name
            packageName = packageName.substring(0, packageName.indexOf(packageName.match(/\d+/)[0]));

            return packageName;
        }
like image 131
Dor Cohen Avatar answered Oct 03 '22 22:10

Dor Cohen