2012-10-15 111 views
4

我可以使用相对路径压缩文件吗?ZipArchives存储绝对路径

例如:

$zip->addFile('c:/wamp/www/foo/file.txt');

的ZIP应该有这样的目录结构:

foo 
-> file.txt 

,而不是:

wamp 
-> www 
    -> foo 
     -> file.txt 

像它在默认情况下...

PS:我完整的代码here(我使用ZipArchive到一个目录的内容压缩成zip文件)

回答

8

addFile()函数定义,可以覆盖存档文件名:

$zip->addFile('/path/to/index.txt', 'newname.txt'); 
+0

oh tx。我怎样才能对空目录做同样的事情?它看起来像addEmptyDir函数没有第二个参数? – Alex

+1

只需传递新的目的地名称即可。也就是说,如果你的源代码是/ path/to/empty,那么只需调用'$ zip-> addEmptyDir(“empty”);' –

+0

doh :)谢谢:D – Alex

3

如果您尝试递归添加文件夹的所有子文件夹和文件,则可以尝试下面的代码(我修改了this code/note in the php manual)。

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 

所以基本上这样做是它要压缩,而是只从实际文件夹,你想压缩(相对路径)开始实际的目录/文件夹之前不包括目录(绝对路径) 。

+0

非常感谢这个作品!比我见过的使用PHP递归压缩当前文件夹的其他代码片段更好。 – gaborous

+0

实际上,该解决方案不存储点文件(如.htaccess),因为GLOB会自动对其进行过滤。 GLOB有一个解决方法,但你也可以使用scandir()或opendir(),它实际上更快。我会在下面发布一个代码。 – gaborous

+0

谢谢,请发帖然后我会更新我的答案。 – Paolo

1

这里是Paolo脚本的修改版本,以便还包含点文件,如.htaccess,并且它应该会更快一些,因为我替换了glob by opendir as adviced here

<?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文件,这也是一项安全措施。