ZipArchive文件无效

4
我需要一个简单的函数,将文件数组写入一个zip文件中。我在网上教程中找到了一些代码,并稍作修改,但似乎无法使其正常工作。它可以创建zip文件,但当我尝试解压缩时,会出现错误:
Windows cannot complete the extraction.
The Compressed (zipped) Folder '...' is invalid.

以下是我正在使用的代码:

public function create_zip($files = array(),$destination = '',$overwrite = false) {
      //if the zip file already exists and overwrite is false, return false
      if(file_exists($destination) && !$overwrite) { return 'file exists'; }

      $valid_files = array();
      if(is_array($files)) {
        //cycle through each file
        foreach($files as $file) {
          //make sure the file exists
          if(file_exists($file)) {
            $valid_files[] = $file;
          }
        }
      }
      Zend_Debug::dump($valid_files);

      if(count($valid_files)) {
        //create the archive
        $zip = new ZipArchive();
        if($zip->open($destination, ZIPARCHIVE::CREATE) !== true) {
          return 'could not open zip: '.$destination;
        }
        //add the files
        foreach($valid_files as $file) {
          $zip->addFile($file);
        }
        //debug
        Zend_Debug::dump($zip->numFiles);
        Zend_Debug::dump($zip->status);

        $zip->close();

        //check to make sure the file exists
        return file_exists($destination);
      } else  {
        return 'no valid failes'. count($valid_files);
      }
}

调试语句输出如下内容:
 For $valid_files - array of one file name (full path to file)
 For $zip->numFiles - 1
 For $zip->status - 0
 The function returns true.

有什么想法,我做错了什么吗?
1个回答

4

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接