在手册里搜搜zip~~~
<?php$zip = new ZipArchive();
$filename = "./test.zip";if ($zip->open($filename, ZIPARCHIVE::CREATE)!==TRUE) {
exit("cannot open <$filename>\n");
}$zip->addFile($thisdir . "/a.jpg","/a.jpg");
echo "numfiles: " . $zip->numFiles . "\n";
echo "status:" . $zip->status . "\n";
$zip->close();
//剩下的部分就是下载的代码了~给用户一个链接就可以了。
echo "<a href = 'test.zip'>download</a>"
?>
<?php$zip = new ZipArchive();
$filename = "./test.zip";if ($zip->open($filename, ZIPARCHIVE::CREATE)!==TRUE) {
exit("cannot open <$filename>\n");
}$zip->addFile($thisdir . "/a.jpg","/a.jpg");
echo "numfiles: " . $zip->numFiles . "\n";
echo "status:" . $zip->status . "\n";
$zip->close();
//剩下的部分就是下载的代码了~给用户一个链接就可以了。
echo "<a href = 'test.zip'>download</a>"
?>
据说……某个版本php的zip库,当你解压路径../../../../../file的情况,他会写到别的目录里去。压缩文件好像没事儿。
class CZip
{
var $savePath; //解包存放的路径,为绝对路径
var $zipFullPath; //ZIP包的完整路径
var $srcPath; //zip包所在的路径,必须为绝对路径
var $zipName; //zip包名
var $errCode; //错误信息
var $zipFile = array(); //zip的文件名
function CZip($zip, $savePath = "./")
{
$this->savePath = $savePath;
//获得ZIP包名
if(preg_match("/([[:alpha:]]:[\/\w]*)\/(\w*.zip$)/i", $zip, $match))
{
$this->srcPath = $match[1] . "/";
$this->zipName = $match[2];
$this->zipFullPath = $zip;
}
else
{
$this->errCode = "无效的文件";
}
}
//解压缩文件
function unzip()
{
if(file_exists($this->zipFullPath) && $this->errorCode != "")
{
if(!file_exists($this->savePath))
mkdir($this->savePath, 0777, true);
$zip = zip_open($this->zipFullPath);
if($zip)
{
while ($zip_entry = zip_read($zip))
{
//获得ZIP下的文件或路录名
$name = zip_entry_name($zip_entry);
//建立文件名中包含的路径
$curDir = $this->pMakeDir($name, $this->savePath);
//打开ZIP包,以只读的方式
if (zip_entry_open($zip, $zip_entry, "r"))
{
//读取ZIP包中的内容
$buf = zip_entry_read($zip_entry, zip_entry_filesize($zip_entry));
//如果是文件,则建立新文件
if(!preg_match("/\/$/", $name))
{
$handle = fopen($this->savePath . $name,'a+');
fwrite($handle, $buf);
fclose($handle);
zip_entry_close($zip_entry);
$this->zipFile[] = $name;
}
}
}
zip_close($zip);
return true;
}
else
{
$this->errCode = "无效的ZIP包";
return false;
}
}
return false;
}
/***
函数名: delZip
函数功能:删除ZIP包
**/
function delZip()
{
if(file_exists($this->zipFullPath))
{
unlink($this->zipFullPath);
}
}
}使用php_zip扩展