php空间解压代码

  • A+
所属分类:PHP技术

下面"a.zip"表示你需要把压缩包文件夹改为a,且类型一定需要是zip
<?php
$zip = zip_open("a.zip");
if ($zip) {
while ($zip_entry = zip_read($zip)) {
$fp = fopen(zip_entry_name($zip_entry), "w");
if (zip_entry_open($zip, $zip_entry, "r")) {
$buf = zip_entry_read($zip_entry, zip_entry_filesize($zip_entry));
fwrite($fp,"$buf");
zip_entry_close($zip_entry);
fclose($fp);
}
}
zip_close($zip);
}
?>

下面代码实现了把改文件所有文件压缩为一个zip文件以时间命名:
<html>
<head>
<title>打包操作中...</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<?php
set_time_limit(0);
$zip = new ZipArchive();
$filename = "./".date("Y-m-d-H-i-s-").md5(rand(10000,99999))."_best33backup.zip";
if ($zip->open($filename, ZIPARCHIVE::CREATE)!==TRUE) {
die("当前目录不可写。创建文件失败,请检查目录权限。");
}
$files = listdir();
foreach($files as $path){
$zip->addFile($path,str_replace("./","",str_replace("\\","/",$path)));
}
echo "打包完成,共处理" . $zip->numFiles . "个文件。<br />请到当前目录下检查以日期为前缀命名的zip文件!";
$zip->close();
function listdir($start_dir='.') {
$files = array();
if (is_dir($start_dir)) {
$fh = opendir($start_dir);
while (($file = readdir($fh)) !== false) {
if(strcmp($file, '.')==0 || strcmp($file, '..')==0){
continue;
}
$filepath = $start_dir . '/' . $file;
if(is_dir($filepath)){
$files = array_merge($files, listdir($filepath));
}else{
array_push($files, $filepath);
}
}
closedir($fh);
}else{
$files = false;
}
return $files;
}
?>
</body>
</html>

发表评论

您必须登录才能发表评论!