php ziparchive 报错 Zip archive inconsistent

代码如下,使用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]$

阅读 4.9k
1 个回答
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题