分享

Android 解压APK文件

 勤奋不止 2015-03-23

最近一个项目里需要得到Android已安装程序的签名文件(CERT.RSA)的SHA1值,所以就调研解压APK文件。

  • 通过包名来得到已安装程序在系统的安装包路径。
  1. private static String getApkPath(String pkgName) {  
  2.         PackageManager pm = mContext.getPackageManager();  
  3.         ApplicationInfo pi = null;  
  4.         try {  
  5.             pi = pm.getApplicationInfo(pkgName,PackageManager.GET_UNINSTALLED_PACKAGES);  
  6.             if(pi != null)  
  7.                 return pi.sourceDir;  
  8.             else   
  9.                 return null;  
  10.         } catch (NameNotFoundException e) {  
  11.             e.printStackTrace();  
  12.             return null;  
  13.         }  
  14.     }  
  • 解压对应的APK包,得到需要的文件(.RSA)
  1. public static void UnZip(String unzipfile){  
  2.               try {     
  3.                 File zipFile = new File(unzipfile); //解压缩的文件路径(为了获取路径)   
  4.                 if(!zipFile.exists())  
  5.                 {  
  6.                     Log.i(TAG,"FILE !EXIST");  
  7.                     return ;  
  8.                 }  
  9.                 ZipInputStream zin = new ZipInputStream(new FileInputStream(zipFile));     
  10.                 ZipEntry entry;     
  11.                 while ( (entry = zin.getNextEntry()) != null){  
  12.                   if (!entry.isDirectory()) { //匹配文件,跳过文件夹    
  13.                      String filePath = entry.getName();  
  14.                      Pattern p = Pattern.compile(".*(RSA|DSA|rsa)$"); //匹配RSA后缀的文件    
  15.                      Matcher m = p.matcher(filePath);  
  16.                      if(m.matches())  
  17.                      {    
  18.                          ByteArrayOutputStream baos = new ByteArrayOutputStream();   
  19.                          byte[] buffer = new byte[1024];     
  20.                          int count;         
  21.                          while ((count = zin.read(buffer)) != -1) {  
  22.                                  baos.write(buffer, 0, count);       
  23.                              }  
  24.                            
  25.                         try {  
  26.                             String sha1 = Sha1.getSha1(baos);  
  27.                             Log.i(TAG, "Sha1:"+sha1+"");  
  28.                         } catch (Exception e) {  
  29.                             e.printStackTrace();  
  30.                         }  
  31.                             }   
  32.                          }   
  33.                 }//while  
  34.                 zin.closeEntry();     
  35.               }//try     
  36.               catch (IOException e) {     
  37.                 e.printStackTrace();     
  38.               }  
  39.           }  

主要就是通过ZipInputStream来读取对应文件,然后将该文件写到SD卡上,然后调用sha1方法读取该文件得到sha1值。

ZipInputStream类,比较重要,值得学习一下。

SDK里:

总结:ZipInputStream是InputStream的子类,通过此类可以方便地读取ZIP格式的压缩文件。

通过ZipInputStream类中的getNextEntry()方法可以依次取得每一个ZipEntry,那么将此类与ZipFile结合就可以对压缩的文件夹进行解压缩操作。但是需要注意的是,在mldndir.zip文件中本身是包含压缩的文件夹的,所以在进行解压缩前,应该先根据ZIP文件中的文件夹的名称在硬盘上创建好一个对应的文件夹,然后才能把文件解压缩进去,而且在操作时对于每一个解压缩的文件都必须先创建(File类的createNewFile()方法可以创建新文件)后再将内容输出。

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多