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

Mos*_*ady 4 php

如何通过php创建一个空的zip archve像new - > zip archive

Cro*_*zin 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();
Run Code Online (Sandbox Code Playgroud)

您可以在PHP的手册页上找到更多信息.


Fra*_*sas 5

创建“.” 目录似乎对我有用:

$zip->addEmptyDir('.');
Run Code Online (Sandbox Code Playgroud)

在某些应用程序中,它会在列出 ZIP 中的文件时出现,但在解压时不会出现。


小智 5

我使用此代码从要压缩的文件数组中返回一个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
  }
}
Run Code Online (Sandbox Code Playgroud)