Java parsing IPA file, reading info.plist information

Source: Internet
Author: User
Tags create zip create zip file file info

When it comes to mobile MDM, the question is, how do I know about this IPA file when the user uploads the IPA file? IPA file has a very important file info.plist similar to the Android program Manifest.xml, as long as the ability to extract from the IPA file Info.plist, and then in the analysis of the relevant information, it is a success. So, start with the above steps.

1, Java Extract IPA file, only get Info.plist

/** *  Extract the IPA file, get only the Info.plist file of the IPA file to store the specified location  *  @param  file * zip file  *   @param  unzipDirectory *  extracted directories  *  @throws  Exception */private  Static file getzipinfo (file file, string unzipdirectory) throws Exception {/ /  defines the input and output stream object inputstream input = null;outputstream output = null; file result = null; file unzipfile = null; zipfile zipfile = null;try {//  Create zip file object Zipfile = new zipfile (file);//   Create this zip file to extract the directory String name = file.getname (). substring (0,file.getname (). LastIndexOf (".")); Unzipfile = new file (unzipdirectory +  "/"  + name);if  (unzipFile.exists ()) {Unzipfile.delete ();} Unzipfile.mkdir ();//  Gets the ZIP file entry enumeration Object Enumeration<zipentry> zipenum = zipfile.getentries ();//  Define object Zipentry entry = nUll string entryname = null; string names[] = null;int length;//  Loop Read Entry while  (Zipenum.hasmoreelements ())  {//  Get current entry entry = zipenum.nextelement (); Entryname = new string ( Entry.getname ());//  use/Separate entry name Names = entryname.split ("\\/"); length = names.length;for   (int v = 0; v < length; v++)  {if (Entryname.endswith (". app/ Info.plist ")) { //  is info.plist file, output to file Input = zipfile.getinputstream (entry); result =  new file (Unzipfile.getabsolutepath () +  "/info.plist");output = new  FileOutputStream (result); Byte[] buffer = new byte[1024 * 8];int readlen  = 0;while  ((Readlen = input.read (buffer, 0, 1024 * 8))  !=  -1) {output.write (Buffer, 0, readlen);} Break;}}}}  catch  (Exception ex)  {ex.printstAcktrace ();}  finally {if  (Input != null) input.close ();if  (output != null)  { Output.flush (); Output.close ();}   The stream must be turned off, otherwise the file cannot be deleted if (zipfile != null) {Zipfile.close ()}}   If necessary delete redundant files if (file.exists ()) {File.delete ();} Return result;}
Copy of the/** * ipa file, copy an IPA file as a zip file, and return info.plist files  *  parameters  oldfile  for  ipa files   */private static file getipainfo (File oldfile)  throws ioexception {try{int  byteread = 0; String filename = oldfile.getabsolutepath (). ReplaceAll (". IPA",  ". zip"); File newfile = new file (filename);if  (oldfile.exists ()) {//  Create a zip file InputStream  instream = new fileinputstream (Oldfile); Fileoutputstream fs = new fileoutputstream (NewFile);      byte[]  buffer = new byte[1444];             while  (byteread = instream.read (buffer)  != -1) {Fs.write (buffer,0, Byteread);                    }if (instream != null) {instream.close ();}if (fs != null) {fs.close ();}   Parsing zip file Return unzip (newfile, newfile.getparent ());}            }catch (exception e) {        e.printstacktrace ();     }return null;}

2, Java read the Info.plist file, get the required information

/** *  Get info info via IPA file  *  This method can be refactored, because it means getting some important information and if you want to get it all, you should return a map<string,object>  *  for the array information in the plist file should be serialized stored in the map, then only the third jar provided by the Nsarray can do!  */public static map<string,string> getipainfomap (FILE&NBSP;IPA)  throws  Exception{map<string,string> map = new hashmap<string,string> (); File file = getipainfo (IPA);//  Third party jar package provides nsdictionary rootdict =  ( nsdictionary)  propertylistparser.parse (file);//  app package name nsstring parameters =  (NSString)  rootdict.objectforkey ("Cfbundleidentifier"); Map.put ("Cfbundleidentifier",  parameters.tostring ());//   Application name parameters =  (nsstring)  rootdict.objectforkey ("Cfbundlename"); Map.put ("CFBundleName",  parameters.tostring ());//  application version parameters =  (nsstring)  rootdict.objectforkey (" Cfbundleversion "); Map.put (" Cfbundleversion ",  parameters.tostring ());//  the name of the application display Parameters =  (NSString)  rootdict.objectforkey ("Cfbundledisplayname"); Map.put (" Cfbundledisplayname ",  parameters.tostring ());//  app required iOS minimum version parameters =  (NSString)   Rootdict.objectforkey ("Minimumosversion"); Map.put ("Minimumosversion",  parameters.tostring ());//  If necessary, you should delete the extracted results file File.delete (); File.getparentfile (). Delete (); return map;}

3. Program Test

public static void Main (string[] args) throws Exception {File File = new file ("D:/uniaccess.ipa"); map<string,string> map = getipainfomap (file), for (String Key:map.keySet ()) {System.out.println (key+ ":" +map.get (key));}}

4. Test results

CFBUNDLEIDENTIFIER:COM.QIHOO.INSTALLSAFETYCFBUNDLEDISPLAYNAME:360 Mobile Defender Cfbundlename:360mobilesafecfbundleversion : 4.2.0.2minimumosversion:5.0

5, the relevant jar package, as well as the IPA file, can be downloaded to the address specified below

1) http://download.csdn.net/detail/wp562846864/8474481

2) Http://m1.app111.org/2014/09/19/20140919142959.ipa


Java parsing IPA file, reading info.plist information

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.