开发者

how to create an empty zip archve by php

how to cr开发者_StackOverflow社区eate an empty zip archve by php some thing like new -> zip archive


I use this code to return a zip file from an array of files to be zipped, if the array is empty, than it returns an empty zip archive, hope it helps.

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
  }
}


You were pretty close:

$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();

You can find out more on PHP's manual pages.


Creating the '.' directory seems to work for me:

$zip->addEmptyDir('.');

It appears in some applications when listing the files in the ZIP but not when extracting it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜