php ziparchive 报错 Zip archive inconsistent
黄舟
黄舟 2017-04-10 17:34:26
[PHP讨论组]

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

黄舟
黄舟

人生最曼妙的风景,竟是内心的淡定与从容!

全部回复(1)
迷茫

可能php版本问题 5.6不支持
官方备注说:
http://php.net/manual/zh/zipa...

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号