当前位置: 编程技术>php
php获取apk包信息的方法
来源: 互联网 发布时间:2014-08-26
本文导语: 有时候在使用php上传安卓apk包的时候,我们需要获取安卓apk包内的信息,本文以实例形式讲述了php获取apk包信息的方法。具体实现方法如下: 以下是Apkparser类包,把以下代码复制出来保存为Apkparser.php就可以执行以上代...
有时候在使用php上传安卓apk包的时候,我们需要获取安卓apk包内的信息,本文以实例形式讲述了php获取apk包信息的方法。具体实现方法如下:
以下是Apkparser类包,把以下代码复制出来保存为Apkparser.php就可以执行以上代码
0 ? $this->getStringTab($strOffset, $strListOffset) : array(); $this->styleTab = $this->styleCount > 0 ? $this->getStringTab($styOffset, $styListOffset) : array(); $o = $size; break; case self::RESOURCEIDS: $count = $size / 4 - 2; $this->resourceIDs = $this->get32array($o, $count); break; case self::START_NAMESPACE: $o += 8; $prefix = $this->get32($o); $uri = $this->get32($o); if (empty($this->cur_ns)){ $this->cur_ns = array(); $this->ns[] = &$this->cur_ns; } $this->cur_ns[$uri] = $prefix; break; case self::END_NAMESPACE: $o += 8; $prefix = $this->get32($o); $uri = $this->get32($o); if (empty($this->cur_ns)) break; unset($this->cur_ns[$uri]); break; case self::START_TAG: $line = $this->get32($o); $o += 4; $attrs = array(); $props = array( 'line' => $line, 'ns' => $this->getNameSpace($this->get32($o)), 'name' => $this->getString($this->get32($o)), 'flag' => $this->get32($o), 'count' => $this->get16($o), 'id' => $this->get16($o)-1, 'class' => $this->get16($o)-1, 'style' => $this->get16($o)-1, 'attrs' => &$attrs ); $props['ns_name'] = $props['ns'].$props['name']; for ($i=0; $i < $props['count']; $i++){ $a = array( 'ns' => $this->getNameSpace($this->get32($o)), 'name' => $this->getString($this->get32($o)), 'val_str' => $this->get32($o), 'val_type' => $this->get32($o), 'val_data' => $this->get32($o) ); $a['ns_name'] = $a['ns'].$a['name']; $a['val_type'] >>= 24; $attrs[] = $a; } // 处理TAG字符串 $tag = "getAttributeValue($a). '"'; } $tag .= '>'; $props['tag'] = $tag; unset($this->cur_ns); $this->cur_ns = array(); $this->ns[] = &$this->cur_ns; $left = -1; break; case self::END_TAG: $line = $this->get32($o); $o += 4; $props = array( 'line' => $line, 'ns' => $this->getNameSpace($this->get32($o)), 'name' => $this->getString($this->get32($o)) ); $props['ns_name'] = $props['ns'].$props['name']; $props['tag'] = ""; if (count($this->ns) > 1){ array_pop($this->ns); unset($this->cur_ns); $this->cur_ns = array_pop($this->ns); $this->ns[] = &$this->cur_ns; } break; case self::TEXT: $o += 8; $props = array( 'tag' => $this->getString($this->get32($o)) ); $o += 8; break; default: throw new Exception('Block Type Error', 3); break; } $this->skip($o); $child = array(); while ($this->length > $left){ $c = $this->parseBlock(); if ($props && $c) $child[] = $c; if ($left == -1 && $c['type'] == self::END_TAG){ $left = $this->length; break; } } if ($this->length != $left) throw new Exception('Block Overflow Error', 4); if ($props){ $props['type'] = $type; $props['size'] = $size; $props['child'] = $child; return $props; }else { return false; } } private function getAttributeValue($a){ $type = &$a['val_type']; $data = &$a['val_data']; switch ($type){ case self::TYPE_STRING: return $this->getString($a['val_str']); case self::TYPE_ATTRIBUTE: return sprintf('?%s%08X', self::_getPackage($data), $data); case self::TYPE_REFERENCE: return sprintf('@%s%08X', self::_getPackage($data), $data); case self::TYPE_INT_HEX: return sprintf('0x%08X', $data); case self::TYPE_INT_BOOLEAN: return ($data != 0 ? 'true' : 'false'); case self::TYPE_INT_COLOR_ARGB8: case self::TYPE_INT_COLOR_RGB8: case self::TYPE_INT_COLOR_ARGB4: case self::TYPE_INT_COLOR_RGB4: return sprintf('#%08X', $data); case self::TYPE_DIMENSION: return $this->_complexToFloat($data).self::$DIMENSION_UNITS[$data & self::UNIT_MASK]; case self::TYPE_FRACTION: return $this->_complexToFloat($data).self::$FRACTION_UNITS[$data & self::UNIT_MASK]; case self::TYPE_FLOAT: return $this->_int2float($data); } if ($type >=self::TYPE_INT_DEC && $type < self::TYPE_INT_COLOR_ARGB8){ return (string)$data; } return sprintf('', $data, $type); } private function _complexToFloat($data){ return (float)($data & 0xFFFFFF00) * self::$RADIX_MULTS[($data>>4) & 3]; } private function _int2float($v) { $x = ($v & ((1 31 | 1); $exp = ($v >> 23 & 0xFF) - 127; return $x * pow(2, $exp - 23); } private static function _getPackage($data){ return ($data >> 24 == 1) ? 'android:' : ''; } private function getStringTab($base, $list){ $tab = array(); foreach ($list as $off){ $off += $base; $len = $this->get16($off); $mask = ($len >> 0x8) & 0xFF; $len = $len & 0xFF; if ($len == $mask){ if ($off + $len > $this->length) throw new Exception('String Table Overflow', 11); $tab[] = substr($this->xml, $off, $len); }else { if ($off + $len * 2 > $this->length) throw new Exception('String Table Overflow', 11); $str = substr($this->xml, $off, $len * 2); $tab[] = mb_convert_encoding($str, 'UTF-8', 'UCS-2LE'); } } return $tab; } private function getString($id){ if ($id > -1 && $id < $this->stringCount){ return $this->stringTab[$id]; }else { return ''; } } private function getNameSpace($uri){ for ($i=count($this->ns); $i > 0; ){ $ns = $this->ns[--$i]; if (isset($ns[$uri])){ $ns = $this->getString($ns[$uri]); if (!empty($ns)) $ns .= ':'; return $ns; } } return ''; } private function get32(&$off){ $int = unpack('V', substr($this->xml, $off, 4)); $off += 4; return array_shift($int); } private function get32array(&$off, $size){ if ($size xml, $off, 4 * $size)); if (count($arr) != $size) throw new Exception('Array Size Error', 10); $off += 4 * $size; return $arr; } private function get16(&$off){ $int = unpack('v', substr($this->xml, $off, 2)); $off += 2; return array_shift($int); } private function skip($size){ $this->xml = substr($this->xml, $size); $this->length -= $size; } } //--------------------- //Apkparser类包结束 //--------------------- ?>
感兴趣的朋友可以调试运行一下本文实例,相信会对大家的php程序开发带来一定的启发。