微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

我使用 PHP 的 ZipArchive 创建的 .zip 存档在 Windows 10 上已损坏/无效

如何解决我使用 PHP 的 ZipArchive 创建的 .zip 存档在 Windows 10 上已损坏/无效

我正在使用以下函数来创建我的 MysqL 数据库的备份。这确实很好用,它会生成一个包含所有内容.sql 文件。该文件本身最近大约为 36 兆字节。我希望将文件压缩为 zip 文件以帮助节省一些磁盘空间。我可以创建 .zip 并且它似乎可以工作并创建了大约 4.5 兆字节的 .zip 文件。但是,我尝试下载 zip 文件并在 Windows 10 上打开它,我得到了:

这个文件夹是空的。

当我尝试解压缩 zip 时,即使它是空的,我也得到:

Windows 无法完成提取
压缩(zipped)文件夹 {filepath} 无效。

我使用了 echo 'The zip archive contains ',$zip->numFiles,' files with a status of ',$zip->status;,它给了我:

zip 存档包含 1 个状态为 0 的文件

使用 ZipArchive 打开 zipfile 和 var dumping 结果给了我:

object(ZipArchive)[15]
  public 'status' => int 0
  public 'statusSys' => int 0
  public 'numFiles' => int 1
  public 'filename' => string '/backups/MysqL/2021-01-01-0258-MysqL_backup_1609491536.zip' (length=91)
  public 'comment' => string '' (length=0)

我在做什么破坏了我无法解压缩或在 Windows 上查看文件文件PHP 似乎能够告诉文件在那里。

        function backup_tables($savepath,$tables = '*',$zip=true) {
            $savepath = rtrim($savepath,'/');
            $filepath = $savepath.'/';
            $name = date("Y-m-d-hi").'-MysqL_backup_'.time();
            $fileName = $filepath.$name.'.sql';
            $zipName = $filepath.$name.'.zip';
            //$this->select("SET NAMES 'utf8'");
            
            exec('MysqLdump --user='.escapeshellarg($this->user).' --password='.escapeshellarg($this->pass).' --host='.escapeshellarg($this->host).' '.escapeshellarg($this->db).' > '.escapeshellarg($fileName).'');
            if(file_exists($fileName)) {
            //if(fclose($handle)){
                if($zip==true) {
                    $zip = new ZipArchive();
                    if($zip->open($zipName,ZIPARCHIVE::CREATE) !== true) {
                        return false;
                    }
                    //add the files
                    
                    $zip->addFile($fileName);
                    //debug
                    //echo 'The zip archive contains ',$zip->status;
                    //close the zip -- done!
                    $zip->close();
                    if(file_exists($zipName)) {
                        //unlink($fileName);
                    }
                    
                    
                    $za = new ZipArchive();
                    $za->open($zipName);
                    var_dump($za);
                    
                    /* All good for Zip but return nothing. */
                    return true;
                }
                else {
                    /* All good for .sql file but return nothing. */
                    return true;
                }
                exit; 
            }
            else {
                echo 'sql file does not exists at: '.$fileName;
            }
        }

更新

正如答案中所指出的,我在打开文件时输入了错误的变量名。我现在在 var_dump($za) 上得到了这个。

 object(ZipArchive)[15]
    public 'status' => int 0
    public 'statusSys' => int 0
    public 'numFiles' => int 1
    public 'filename' => string '.../backups/MysqL/2021-01-01-0313-MysqL_backup_1609492418.zip' (length=91)
    public 'comment' => string '' (length=0)

现在说里面有一个文件;但是它在 Windows 上仍然损坏。

解决方法

您在支票中打开 $fileName,但您必须打开 $zipName...

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。