如何通过php创建一个空的zip存档

时间:2010-08-16 19:36:03

标签: php

如何通过php创建一个空的zip archve 就像是 新的 - > zip存档

3 个答案:

答案 0 :(得分:6)

你非常接近:

$zip = new ZipArchive();
$zip->open('my-archive.zip', ZipArchive::CREATE);

// To actually save the archive you have to put some file/dir into it
$zip->addFromString('tmp', '');
$zip->close();

您可以在PHP's manual pages上找到更多信息。

答案 1 :(得分:3)

创建'。'目录似乎对我有用:

$zip->addEmptyDir('.');

在某些应用程序中,当列出ZIP中的文件时会出现,但在提取文件时则不会。

答案 2 :(得分:3)

我使用此代码从要压缩的文件数组中返回一个zip文件,如果该数组为空,则它将返回一个空的zip存档,希望对您有所帮助。

function zipFiles($files){
  // "UEsFBgAAAAAAAAAAAAAAAAAAAAAAAA==" is the base64 encoded content of an empty zip archive
  if(count($files) == 0){
    return base64_decode("UEsFBgAAAAAAAAAAAAAAAAAAAAAAAA=="); 
  }
  $zip = new ZipArchive();
  ... do your stuff
  foreach($files as $file){
    ... do your stuff
  }
}