IOS 文件解析

时间:2023-03-08 16:08:19
 import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream; import org.apache.commons.lang.StringUtils;
import org.apache.poi.util.IOUtils;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.w3c.dom.Node;
import org.xml.sax.InputSource; /**
* IOS文件解析
*
* @author dKF63325
* @version ONIP BME V300R001 2014-6-9
* @since ONIP BME V300R001C00
*/
public class IosInfoUtils
{ public static HashMap<String, Object> getIosInfo(String filePath, String fileName) throws Exception
{
// 获得二级目录名称
String appName = getAppName(filePath); // 解析文件
HashMap<String, Object> infoMap = parseXml(filePath, appName); File file = new File(filePath);
// 文件名称
infoMap.put("fileName", file.getName());
// 文件大小
infoMap.put("fileSize", convertFileSize(file.length()));
// 文件大小(单位:字节)
infoMap.put("fileByteSize", file.length());
// 是否存在SDK
infoMap.put("isSDK", AXMLPrinter2.isExistsSdkFromIOS(filePath, appName));
// SDK版本号
infoMap.put("sdkVersion", AXMLPrinter2.getSdkVersionFromIOS(filePath, appName)); return infoMap;
} private static String getAppName(String filePath)
{
ZipFile file = null;
InputStream in = null;
String name = StringUtils.EMPTY;
try
{
in = new BufferedInputStream(new FileInputStream(filePath));
ZipInputStream zip = new ZipInputStream(in);
ZipEntry zp = null;
while ((zp = zip.getNextEntry()) != null)
{
name = zp.getName();
if (name.indexOf(".app") != -1)
{
name = name.substring(name.indexOf("Payload/")+"Payload/".length(), name.indexOf(".app") + ".app".length());
break;
}
}
}
catch (IOException e)
{
DEBUGGER.error("Failed to getAppName", e);
}
finally
{
IOUtils.closeQuietly(in);
AXMLPrinter2.closeZipFile(file);
}
return name;
} private static HashMap<String, Object> parseXml(String filePath, String projectName) throws IOException, Exception, DocumentException
{
String xml = AXMLPrinter2.getXmlFromIOS(filePath, projectName);
StringReader read = new StringReader(xml);
InputSource scource = new InputSource(read);
SAXReader sax = new SAXReader();
sax.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
Document root = sax.read(scource);
Element dictElement = (Element) root.selectSingleNode("//dict");
List<Element> children = filterElement(dictElement.elements());
HashMap<String, Object> infoMap = new HashMap<String, Object>();
for (int i = 0; i < children.size(); i+=2)
{
Element key = children.get(i);
Element val = children.get(i+1);
if ("array".equals(val.getName()))
{
List<Element> arrayList = filterElement(val.elements());
String values = StringUtils.EMPTY;
for (Element element : arrayList)
{
values += element.getStringValue() + ",";
}
infoMap.put(key.getStringValue(), StringUtils.removeEnd(values, ","));
} else if ("true".equals(val.getName())) {
infoMap.put(key.getStringValue(), true);
} else if ("false".equals(val.getName())) {
infoMap.put(key.getStringValue(), false);
} else {
infoMap.put(key.getStringValue(), val.getStringValue());
}
}
return infoMap;
} private static List<Element> filterElement(List<?> elements) {
List<Element> result = new ArrayList<Element>(elements.size());
for (Object object : elements)
{
Element element = (Element)object;
if (element.getNodeType() == Node.ELEMENT_NODE)
{
result.add(element);
}
}
return result;
} public static String convertFileSize(long filesize)
{
String strUnit = "Bytes";
String strAfterComma = "";
int intDivisor = 1;
if (filesize >= 1024 * 1024)
{
strUnit = "MB";
intDivisor = 1024 * 1024;
} else if (filesize >= 1024)
{
strUnit = "KB";
intDivisor = 1024;
}
if (intDivisor == 1){
return filesize + " " + strUnit;
} strAfterComma = "" + 100 * (filesize % intDivisor) / intDivisor;
if (strAfterComma.equals(""))
strAfterComma = ".0"; return filesize / intDivisor + "." + strAfterComma + " " + strUnit;
}
}