我可以使用相对路径压缩文件吗?
例如:
$zip->addFile('c:/wamp/www/foo/file.txt');
ZIP 应具有如下目录结构:
foo
-> file.txt
并不是:
wamp
-> www
-> foo
-> file.txt
就像默认情况下...
ps:我的完整代码在这里(我正在使用 ZipArchive 将目录的内容压缩到一个 zip 文件中)
我可以使用相对路径压缩文件吗?
例如:
$zip->addFile('c:/wamp/www/foo/file.txt');
ZIP 应具有如下目录结构:
foo
-> file.txt
并不是:
wamp
-> www
-> foo
-> file.txt
就像默认情况下...
ps:我的完整代码在这里(我正在使用 ZipArchive 将目录的内容压缩到一个 zip 文件中)
查看addFile()
函数定义,可以覆盖归档文件名:
$zip->addFile('/path/to/index.txt', 'newname.txt');
如果您尝试递归添加文件夹的所有子文件夹和文件,您可以尝试下面的代码(我在 php 手册中修改了此代码/注释)。
class Zipper extends ZipArchive {
public function addDir($path, $parent_dir = '') {
if($parent_dir != ''){
$this->addEmptyDir($parent_dir);
$parent_dir .= '/';
print '<br>adding dir ' . $parent_dir . '<br>';
}
$nodes = glob($path . '/*');
foreach ($nodes as $node) {
if (is_dir($node)) {
$this->addDir($node, $parent_dir.basename($node));
}
else if (is_file($node)) {
$this->addFile($node, $parent_dir.basename($node));
print 'adding file '.$parent_dir.basename($node) . '<br>';
}
}
}
} // class Zipper
所以基本上它的作用是它不包括您想要压缩的实际目录/文件夹之前的目录(绝对路径),而是仅从您想要压缩的实际文件夹(相对路径)开始。
这是 Paolo 脚本的修改版本,以便还包含 .htaccess 之类的点文件,并且它也应该更快一些,因为我按照此处的建议将 glob 替换为 opendir。
<?php
$password = 'set_a_password'; // password to avoid listing your files to anybody
if (strcmp(md5($_GET['password']), md5($password))) die();
// Make sure the script can handle large folders/files
ini_set('max_execution_time', 600);
ini_set('memory_limit','1024M');
//path to directory to scan
if (!empty($_GET['path'])) {
$fullpath = realpath($_GET['path']); // append path if set in GET
} else { // else by default, current directory
$fullpath = realpath(dirname(__FILE__)); // current directory where the script resides
}
$directory = basename($fullpath); // parent directry name (not fullpath)
$zipfilepath = $fullpath.'/'.$directory.'_'.date('Y-m-d_His').'.zip';
$zip = new Zipper();
if ($zip->open($zipfilepath, ZipArchive::CREATE)!==TRUE) {
exit("cannot open/create zip <$zipfilepath>\n");
}
$past = time();
$zip->addDir($fullpath);
$zip->close();
print("<br /><hr />All done! Zipfile saved into ".$zipfilepath);
print('<br />Done in '.(time() - $past).' seconds.');
class Zipper extends ZipArchive {
// Thank's to Paolo for this great snippet: http://stackoverflow.com/a/17440780/1121352
// Modified by LRQ3000
public function addDir($path, $parent_dir = '') {
if($parent_dir != '' and $parent_dir != '.' and $parent_dir != './') {
$this->addEmptyDir($parent_dir);
$parent_dir .= '/';
print '<br />--> ' . $parent_dir . '<br />';
}
$dir = opendir($path);
if (empty($dir)) return; // skip if no files in folder
while(($node = readdir($dir)) !== false) {
if ( $node == '.' or $node == '..' ) continue; // avoid these special directories, but not .htaccess (except with GLOB which anyway do not show dot files)
$nodepath = $parent_dir.basename($node); // with opendir
if (is_dir($nodepath)) {
$this->addDir($nodepath, $parent_dir.basename($node));
} elseif (is_file($nodepath)) {
$this->addFile($nodepath, $parent_dir.basename($node));
print $parent_dir.basename($node).'<br />';
}
}
}
} // class Zipper
?>
这是一个独立的脚本,只需将其复制/粘贴到一个 .php 文件(例如:zipall.php)并在浏览器中打开它(例如:zipall.php?password=set_a_password
,如果您没有设置正确的密码,页面将保持空白以确保安全)。之后您必须使用 FTP 帐户来检索 zip 文件,这也是一种安全措施。