ZipArchive文件无效

时间:2012-04-30 17:36:29

标签: php zip ziparchive

我需要一个简单的函数,将一个文件数组写入一个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 个答案:

答案 0 :(得分:1)

只需要在文件名之前添加斜杠。

在这里找到答案:using zipArchive addFile() will not add image to zip