Java to extract information such as APK version by parsing files

Source: Internet
Author: User
Tags object object

ImportJava.io.ByteArrayInputStream;Importjava.io.IOException;ImportJava.io.InputStream;Importjava.util.ArrayList;Importjava.util.List;ImportJava.util.zip.ZipEntry;ImportJava.util.zip.ZipFile;Importorg.jdom.Document;Importorg.jdom.Element;ImportOrg.jdom.Namespace;ImportOrg.jdom.input.SAXBuilder;Importcn.zsmy.constant.Constant; Public classApkutil {Private Static FinalNamespace NS = Namespace.getnamespace ("Http://schemas.android.com/apk/res/android"); @SuppressWarnings ({"Unchecked", "Rawtypes"})     Public Staticapkinfo getapkinfo (String apkpath) {apkinfo apkinfo=NewApkinfo (); Saxbuilder Builder=NewSaxbuilder (); Document Document=NULL; Try{document=Builder.build (Getxmlinputstream (Apkpath)); }Catch(Exception e) {e.printstacktrace (); } Element Root= Document.getrootelement ();//-->manifest with nodeApkinfo.setversioncode (Root.getattributevalue ("Versioncode", NS)); Apkinfo.setversionname (Root.getattributevalue ("Versionname", NS)); Apkinfo.setapkpackage (Root.getattributevalue ("Package")); Element ELEMUSESDK= Root.getchild ("Uses-sdk");//child node-->uses-sdkApkinfo.setminsdkversion (Elemusesdk.getattributevalue ("Minsdkversion", NS)); List listpermission= Root.getchildren ("uses-permission");//A child node is a collectionList permissions =NewArrayList ();  for(Object object:listpermission) {String permission= (Element) object. Getattributevalue ("name"), NS);        Permissions.add (permission);        } apkinfo.setuses_permission (permissions); Constant.MY_LOG.debug (\ n Version number: "+apkinfo.getversioncode () +" \ n Version Name: "+apkinfo.getversionname () +" \ n Package Name: "+apkinfo.getapkpackage ()); //String str = "\ n version number:" \ +versioncode+ "\ n version name:" +versionname+ "\ n Package Name:" +packagename;Constant.MY_LOG.debug (Root.getattributes (). toString ()); returnApkinfo;//String s = root.getattributes (). toString ();//String c[] = S.split (",");//String versioncode = null;//String versionname = null;//String packagename = null;//For (String a:c) {//if (a.contains ("Versioncode")) {//Versioncode = a.substring (A.indexof ("versioncode=\") +13, A.lastindexof ("\" "));//            }//if (a.contains ("Versionname")) {//versionname = a.substring (A.indexof ("versionname=\") +13, A.lastindexof ("\" "));//            }//if (a.contains ("package")) {//PackageName = a.substring (A.indexof ("package=\") +9, A.lastindexof ("\" "));//            }//        }        //        //Constant.MY_LOG.debug ("\ n version number:" +versioncode+ "\ nthe version name:" +versionname+ "\ n Package Name:" +packagename ");//String str = "\ n version number:" \ +versioncode+ "\ n version name:" +versionname+ "\ n Package Name:" +packagename;////return Root.getattributes (). toString ();//return str;//return "SS";    }    Private StaticInputStream Getxmlinputstream (String apkpath) {InputStream InputStream=NULL; InputStream Xmlinputstream=NULL; ZipFile ZipFile=NULL; Try{ZipFile=NewZipFile (Apkpath); ZipEntry ZipEntry=NewZipEntry ("Androidmanifest.xml"); InputStream=Zipfile.getinputstream (ZipEntry); Axmlprinter Xmlprinter=NewAxmlprinter ();            Xmlprinter.startprinf (InputStream); Xmlinputstream=NewBytearrayinputstream (Xmlprinter.getbuf (). toString (). GetBytes ("UTF-8")); } Catch(IOException e) {e.printstacktrace (); Try{inputstream.close ();            Zipfile.close (); } Catch(IOException E1) {e1.printstacktrace (); }        }        returnXmlinputstream; }}

Call:

@ResponseBody @RequestMapping (value= "/getapkversion", method =requestmethod.post) PublicString getapkversion (versionform form, Multipartfile apkfile)throwsException {Constant.MY_LOG.debug ("Get the uploaded apk version"); File Apktempfile=NewFile (DictInit.dictMap.get (Constant.Dict.APK_UPLOAD_PATH) + "temp.apk"); //file Apktempfile = new file ("d:\\temp.apk");//for testingApkfile.transferto (Apktempfile); //Get apk InfoApkinfo Apkinfo =NewApkinfo (); Apkinfo=Apkutil.getapkinfo (Apktempfile.getpath ()); returnApkinfo.getversionname (); }    

Java to extract information such as APK version by parsing files

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.