这是我到目前为止创建zip
文件的原因:
$data = $cache;
$file_name = trim(str_replace( '/' , '_', $dl_url), '_')."-$mysitename.zip";
header("Content-Type: application/zip");
header("Content-Disposition: attachment; filename=$file_name");
header("Content-Length: " . strlen($data));
echo $data;
exit;
}
我需要:1。提取旧的zip 2.添加我的文件到旧的zip文件3.zip文件(旧的zip文件和我的文件在一起)并设置我的评论。
答案 0 :(得分:0)
您需要实际使用ZipArchive类,而不使用标题:
<?php
$defTitle = 'untitled_'.date('YmdHis').'.zip';
$file_name = (!empty($_GET['s']))? trim(str_replace( '/' , '_', $_GET['s']), '_').".zip" : $defTitle;
# Create a new Archive
$zip = new ZipArchive;
# Create the file to archive to
$res = $zip->open($file_name, ZipArchive::CREATE);
# If create works
if ($res === TRUE) {
# Add the data (you can add an actual file here if you want to, this example
# is showing how to create a file with string data)
$zip->addFromString('data.txt', $cache);
# Create the comment here
$zip->setArchiveComment('The comment is written here.');
$zip->close();
}
以上示例改编自PHP手册关于保存存档注释:
http://php.net/manual/en/ziparchive.setarchivecomment.php
修改强>
以下是压缩到zip然后在下载后删除的示例。它具有ZipArchive
类的类扩展:
class Zipster extends ZipArchive
{
private $path;
/**
* @description I like to overwrite this with my own method so I can
* make a directory if not already created
*/
public function open($path,$flags = NULL)
{
$this->path = $path;
$dir = pathinfo($this->path,PATHINFO_DIRNAME);
if(!is_dir($dir))
mkdir($dir,0755,true);
# Send back the original method
return parent::open($this->path,ZipArchive::CREATE);
}
public function download($clear=false)
{
# Get the contents of the compiled zip file
$data = file_get_contents($this->path);
# Do your download headers
header("Content-Type: application/zip");
header("Content-Disposition: attachment; filename=".pathinfo($this->path,PATHINFO_BASENAME));
header("Content-Length: ".strlen($data));
echo $data;
# Delete the file if instructed to delete the file
if($clear)
unlink($this->path);
exit;
}
}
$defTitle = 'untitled_'.date('YmdHis');
$file_name = (!empty($_GET['s']))? trim(str_replace( '/' , '_', $_GET['s']), '_') : $defTitle;
# Create a new (modified) zip archive instance
$zip = new Zipster();
# Create the file to archive to
$res = $zip->open(__DIR__.DS.'tempfolder'.DS.$file_name.".zip");
# If create works
if ($res === TRUE) {
# Add the data (you can add an actual file here if you want to, this example
# is showing how to create a file with string data)
$zip->addFromString($file_name.".txt", $cache);
# Create the comment here
$zip->setArchiveComment('The comment is written here.');
$zip->close();
# Remove true to leave the zip file where it is,
# true will delete the file after download
$zip->download(true);
}