IOS 文件解析
Posted 代码就是情人
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了IOS 文件解析相关的知识,希望对你有一定的参考价值。
1 import java.io.BufferedInputStream; 2 import java.io.File; 3 import java.io.FileInputStream; 4 import java.io.IOException; 5 import java.io.InputStream; 6 import java.io.StringReader; 7 import java.util.ArrayList; 8 import java.util.HashMap; 9 import java.util.List; 10 import java.util.zip.ZipEntry; 11 import java.util.zip.ZipFile; 12 import java.util.zip.ZipInputStream; 13 14 import org.apache.commons.lang.StringUtils; 15 import org.apache.poi.util.IOUtils; 16 import org.dom4j.Document; 17 import org.dom4j.DocumentException; 18 import org.dom4j.Element; 19 import org.dom4j.io.SAXReader; 20 import org.w3c.dom.Node; 21 import org.xml.sax.InputSource; 22 23 24 /** 25 * ios文件解析 26 * 27 * @author dKF63325 28 * @version ONIP BME V300R001 2014-6-9 29 * @since ONIP BME V300R001C00 30 */ 31 public class IosInfoUtils 32 { 33 34 35 public static HashMap<String, Object> getIosInfo(String filePath, String fileName) throws Exception 36 { 37 // 获得二级目录名称 38 String appName = getAppName(filePath); 39 40 // 解析文件 41 HashMap<String, Object> infoMap = parseXml(filePath, appName); 42 43 File file = new File(filePath); 44 // 文件名称 45 infoMap.put("fileName", file.getName()); 46 // 文件大小 47 infoMap.put("fileSize", convertFileSize(file.length())); 48 // 文件大小(单位:字节) 49 infoMap.put("fileByteSize", file.length()); 50 // 是否存在SDK 51 infoMap.put("isSDK", AXMLPrinter2.isExistsSdkFromIOS(filePath, appName)); 52 // SDK版本号 53 infoMap.put("sdkVersion", AXMLPrinter2.getSdkVersionFromIOS(filePath, appName)); 54 55 return infoMap; 56 } 57 58 private static String getAppName(String filePath) 59 { 60 ZipFile file = null; 61 InputStream in = null; 62 String name = StringUtils.EMPTY; 63 try 64 { 65 in = new BufferedInputStream(new FileInputStream(filePath)); 66 ZipInputStream zip = new ZipInputStream(in); 67 ZipEntry zp = null; 68 while ((zp = zip.getNextEntry()) != null) 69 { 70 name = zp.getName(); 71 if (name.indexOf(".app") != -1) 72 { 73 name = name.substring(name.indexOf("Payload/")+"Payload/".length(), name.indexOf(".app") + ".app".length()); 74 break; 75 } 76 } 77 } 78 catch (IOException e) 79 { 80 DEBUGGER.error("Failed to getAppName", e); 81 } 82 finally 83 { 84 IOUtils.closeQuietly(in); 85 AXMLPrinter2.closeZipFile(file); 86 } 87 return name; 88 } 89 90 private static HashMap<String, Object> parseXml(String filePath, String projectName) throws IOException, Exception, DocumentException 91 { 92 String xml = AXMLPrinter2.getXmlFromIOS(filePath, projectName); 93 StringReader read = new StringReader(xml); 94 InputSource scource = new InputSource(read); 95 SAXReader sax = new SAXReader(); 96 sax.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false); 97 Document root = sax.read(scource); 98 Element dictElement = (Element) root.selectSingleNode("//dict"); 99 List<Element> children = filterElement(dictElement.elements()); 100 HashMap<String, Object> infoMap = new HashMap<String, Object>(); 101 for (int i = 0; i < children.size(); i+=2) 102 { 103 Element key = children.get(i); 104 Element val = children.get(i+1); 105 if ("array".equals(val.getName())) 106 { 107 List<Element> arrayList = filterElement(val.elements()); 108 String values = StringUtils.EMPTY; 109 for (Element element : arrayList) 110 { 111 values += element.getStringValue() + ","; 112 } 113 infoMap.put(key.getStringValue(), StringUtils.removeEnd(values, ",")); 114 } else if ("true".equals(val.getName())) { 115 infoMap.put(key.getStringValue(), true); 116 } else if ("false".equals(val.getName())) { 117 infoMap.put(key.getStringValue(), false); 118 } else { 119 infoMap.put(key.getStringValue(), val.getStringValue()); 120 } 121 } 122 return infoMap; 123 } 124 125 private static List<Element> filterElement(List<?> elements) { 126 List<Element> result = new ArrayList<Element>(elements.size()); 127 for (Object object : elements) 128 { 129 Element element = (Element)object; 130 if (element.getNodeType() == Node.ELEMENT_NODE) 131 { 132 result.add(element); 133 } 134 } 135 return result; 136 } 137 138 public static String convertFileSize(long filesize) 139 { 140 String strUnit = "Bytes"; 141 String strAfterComma = ""; 142 int intDivisor = 1; 143 if (filesize >= 1024 * 1024) 144 { 145 strUnit = "MB"; 146 intDivisor = 1024 * 1024; 147 } else if (filesize >= 1024) 148 { 149 strUnit = "KB"; 150 intDivisor = 1024; 151 } 152 if (intDivisor == 1){ 153 return filesize + " " + strUnit; 154 } 155 156 strAfterComma = "" + 100 * (filesize % intDivisor) / intDivisor; 157 if (strAfterComma.equals("")) 158 strAfterComma = ".0"; 159 160 return filesize / intDivisor + "." + strAfterComma + " " + strUnit; 161 } 162 }
以上是关于IOS 文件解析的主要内容,如果未能解决你的问题,请参考以下文章
如何使用 Swift 使用此代码片段为 iOS 应用程序初始化 SDK?