可以把 php5ts.dll php_zip.dll php.ini 放在一个目录
<?php
/*
*读list.txt 文件 生成 zip文件的函数 需要zip 库 支持
*list.txt里每一行
*请将list.txt 放到web的根目录或者 指定其 路径
*请将本程序 放到web的根目录或者 指定 生成 zip 文件的 路径
*/
if (!extension_loaded('zip')) { dl('php_zip.dll');}
$file_list="list.txt"; //文件列表
if(!file_exists($file_list))exit('list does not exists!');
$fileArray=file($file_list);
foreach($fileArray as $key => $path)
{
$fileArray[$key] = $path;
}
$tmp_file = add_file2zip($fileArray,"../pic/","pic_root");
echo "/nok: ".$tmp_file." /n";
/*
fileArray Array 需要压缩的文件列表 一维数组形式
basePath 起始目录
preName 保存的文件名前辍
saveFile 保存的文件名
返回压缩后的文件
*/
function add_file2zip($fileArray,$basePath,$preName="",$saveFile="")
{
//错误检查
if(!is_array($fileArray))return('list does not exists!');
if(empty($saveFile))
$saveFile = $preName."_".date("Ymd_H");
$saveFile.=".zip";
if(file_exists($saveFile)){unlink($saveFile);}
//启动压缩
$zip = new ZipArchive();
if ($zip->open($saveFile, ZIPARCHIVE::CREATE)!==TRUE) {
exit("cannot open <$saveFile>/n");
}
//压入文件
foreach($fileArray as $key => $path)
{
$path = trim($path);
if(empty($path))
continue;
if(!$zip->addFile($basePath.$path,$path))
{
echo ">/n [error]".$path." ";
}
else
echo "/nadd: ";
echo $basePath.$path;
}
$zip->close();
return $saveFile;
}
?>