java通過解析檔案擷取apk版本等資訊

來源:互聯網
上載者:User

標籤:getname   bytes   pat   ict   war   lis   ini   stp   port   

 

import java.io.ByteArrayInputStream;import java.io.IOException;import java.io.InputStream;import java.util.ArrayList;import java.util.List;import java.util.zip.ZipEntry;import java.util.zip.ZipFile;import org.jdom.Document;import org.jdom.Element;import org.jdom.Namespace;import org.jdom.input.SAXBuilder;import cn.zsmy.constant.Constant;public class ApkUtil {        private static final Namespace NS = Namespace.getNamespace("http://schemas.android.com/apk/res/android");        @SuppressWarnings({"unchecked", "rawtypes"})    public static ApkInfo getApkInfo(String apkPath){        ApkInfo apkInfo = new ApkInfo();        SAXBuilder builder = new SAXBuilder();        Document document = null;        try{            document = builder.build(getXmlInputStream(apkPath));        }catch (Exception e) {            e.printStackTrace();        }        Element root = document.getRootElement();//跟節點-->manifest        apkInfo.setVersionCode(root.getAttributeValue("versionCode",NS));        apkInfo.setVersionName(root.getAttributeValue("versionName", NS));        apkInfo.setApkPackage(root.getAttributeValue("package"));        Element elemUseSdk = root.getChild("uses-sdk");//子節點-->uses-sdk        apkInfo.setMinSdkVersion(elemUseSdk.getAttributeValue("minSdkVersion", NS));        List listPermission = root.getChildren("uses-permission");//子節點是個集合        List permissions = new ArrayList();        for(Object object : listPermission){            String permission = ((Element)object).getAttributeValue("name", NS);            permissions.add(permission);        }        apkInfo.setUses_permission(permissions);        Constant.MY_LOG.debug("\n版本號碼:"+apkInfo.getVersionCode()+"\n版本名:"+apkInfo.getVersionName()+"\n包名:"+apkInfo.getApkPackage());        //String str = "\n版本號碼:"+versionCode+"\n版本名:"+versionName+"\n包名:"+packageName;        Constant.MY_LOG.debug(root.getAttributes().toString());        return apkInfo;//        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版本號碼:"+versionCode+"\n版本名:"+versionName+"\n包名:"+packageName);//        String str = "\n版本號碼:"+versionCode+"\n版本名:"+versionName+"\n包名:"+packageName;////        return root.getAttributes().toString();//        return str;//        return "ss";    }    private static InputStream getXmlInputStream(String apkPath) {        InputStream inputStream = null;        InputStream xmlInputStream = null;        ZipFile zipFile = null;        try {            zipFile = new ZipFile(apkPath);            ZipEntry zipEntry = new ZipEntry("AndroidManifest.xml");            inputStream = zipFile.getInputStream(zipEntry);            AXMLPrinter xmlPrinter = new AXMLPrinter();            xmlPrinter.startPrinf(inputStream);            xmlInputStream = new ByteArrayInputStream(xmlPrinter.getBuf().toString().getBytes("UTF-8"));        } catch (IOException e) {            e.printStackTrace();            try {                inputStream.close();                zipFile.close();            } catch (IOException e1) {                e1.printStackTrace();            }        }        return xmlInputStream;    }}

 

調用:

@ResponseBody    @RequestMapping(value = "/getApkVersion", method = RequestMethod.POST)    public String getApkVersion(VersionForm form, MultipartFile apkFile) throws Exception {        Constant.MY_LOG.debug("擷取上傳的apk版本");        File apkTempFile = new File(DictInit.dictMap.get(Constant.Dict.APK_UPLOAD_PATH) + "temp.apk");        // File apkTempFile = new File("d:\\temp.apk");//測試用        apkFile.transferTo(apkTempFile);        // 獲得apk資訊        ApkInfo apkInfo = new ApkInfo();        apkInfo = ApkUtil.getApkInfo(apkTempFile.getPath());        return apkInfo.getVersionName();    }    

 

java通過解析檔案擷取apk版本等資訊

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.