代码如下,使用ZipArchive来做压缩包的文件操作.
<?php
namespace EastSea\Tools;
//test
$tool = new ApkPackTool();
$tool->setConfig(['./kzsg_2.1.8_18.apk','./newApk.apk']);
$tool->setBlankFile('./nginx.conf');
$tool->injectFile(['abc.txt','def.txt']);
class ApkPackTool extends \ZipArchive
{
protected $sourceApkFile;
protected $targetApkFile;
protected $_blankFile;
public function __construct()
{
// code...
}
public function setBlankFile($blankFile){
if(!file_exists($blankFile) || !is_readable($blankFile))
throw new \Exception('the blank file '.$blankFile.' dose not exist');
$this->_blankFile = $blankFile;
}
public function setConfig(Array $config)
{
if(count($config)!=2){
throw new \Exception(static::class.' need a config with two elements');
}else {
$this->sourceApkFile = $config[0];
$this->targetApkFile = $config[1];
}
}
public function injectFile($files=false)
{
if ($files !=false && is_array($files)) {
$this->copyTo();
if($this->open($this->targetApkFile,\ZIPARCHIVE::CREATE) === True){
foreach ($files as $key => $value) {
var_dump($value);
var_dump($this->addFile($this->_blankFile," ".basename($this->_blankFile)));
}
var_dump($this->close());
}else{
throw new \Exception('android apk:'.$this->targetApkFile.' can not open');
}
} else {
throw new \Exception('injection file dose not exist');
}
}
public function copyTo()
{
if(copy($this->sourceApkFile,$this->targetApkFile) === false){
throw new \Exception('android apk'.$this->sourceApkFile.' can not copy to '.$this->targetApkFile);
}
}
}
输出信息:
[vagrant@localhost vagrant]$ php ApkPackTool.php
string(7) "abc.txt"
bool(true)
string(7) "def.txt"
bool(true)
PHP Warning: ZipArchive::close(): Zip archive inconsistent in /vagrant/ApkPackTool.php on line 47
bool(false)
[vagrant@localhost vagrant]$
可能php版本问题 5.6不支持

官方备注说:
http://php.net/manual/zh/zipa...